Search Unity

2D bullet problem

Discussion in 'Scripting' started by lightningGamer, Feb 27, 2015.

  1. lightningGamer

    lightningGamer

    Joined:
    Feb 19, 2013
    Posts:
    52
    Hello all, I am trying to make a 2 D game like space invaders. I wanted to have the enemies make a circle around the player and rotate around you. so like imagine a clock with you at the center and each number is an enemy. The problem I am having is I want the enemies to shoot at the player but I can not find a way to make this work. I have a bullet script that does this to move the bullet:
    Code (csharp):
    1.  
    2.     transform.Translate(Vector2.up* Time.deltaTime * speed);
    3.  
    the problem i am having is finding the right angle to set the bullet to when it spawns so it moves towards the player. I have tried all sorts of things from vector2.angle, to messing around with eulerAngles and loads of other things. I have this
    Code (csharp):
    1.  
    2. shotDirection = -(transform.position - player.position);
    3.         Debug.DrawRay(transform.position,shotDirection, Color.red);
    4.  
    which draws a ray from the bullet to the player and seems like it would work perfectly. But I cannot make the bullet angle itself to match that! please help and thank you!
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Have you tried putting your shotDirection in your bullet script instead of Vector2.up?