Search Unity

Rotate shot towards target - Space Shooter

Discussion in 'Physics' started by WowaBeats, May 4, 2016.

  1. WowaBeats

    WowaBeats

    Joined:
    May 1, 2016
    Posts:
    1
    Hello community,
    it seems like i have a little Problem i can't handle. I've made the SpaceShooter tutorial from Unity and everything worked fine. I made some modifications, like when you collect rockets, you can fire them by pressing the secondary fire button. The idea was, that the rocket finds the next Target and flies towards it. Now my problem is, that the rocket itself only flies on the right side of the screen (something could like the middle(0-point of the screen) could be the problem?). Also, my rocket rotates in a very crazy way, which i don't want. I will show yoou some screenshots and the code of the rotation. I hope, you can help me.

    WowaBeats

    Code (CSharp):
    1.  
    2. if(foundEnemy)
    3.   {
    4.   Vector3 targetDir = nextEnemy.transform.position - transform.position;
    5.   float angle = Vector3.Angle(transform.forward, targetDir);
    6.   rb.velocity = transform.forward * rocketSpeed;
    7.   rb.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
    8.  
    9.   }
    10.   else
    11.   {
    12.   rb.velocity = transform.forward * rocketSpeed;
    13.   }
    14.  

    Notice:
    The shot in the red box always rotates in both sides of the angle(i think that are both sides...), so it looks in the game, like the shot rotates in the z-achsis very crazy. You can't see it in the picture because it is the actual frame...