Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I get The game to work correctly?

Discussion in '2D' started by eyalitvin, Jul 22, 2019.

  1. eyalitvin

    eyalitvin

    Joined:
    Jul 22, 2019
    Posts:
    4
    I'm trying to create a game and as a part of it I need a missile. while trying to code it I wrote this:
    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         if (Input.GetKey(moveleft))
    5.         {
    6.             player.transform.Rotate(ro); // rotate left
    7.         } else if (Input.GetKey(moveright))
    8.         {
    9.             player.transform.Rotate(-1 * ro); // rotate right
    10.            
    11.         }
    12.         float rad = player.rotation * Mathf.Deg2Rad;
    13.         float thrustX = Mathf.Cos(rad) * 2;
    14.         float thrustY = Mathf.Sin(rad) * 2;
    15.         thrust = new Vector2(thrustX, thrustY);
    16.         player.velocity = thrust;
    17.  
    18.     }
    19.  
    20.     void OnCollisionEnter2D()
    21.     {
    22.         Debug.Log("Collision!");
    23.     }
    and this system is working fine until I hit the ground and then the missile starts rotating infinitely without input. I'd really appreciate some help...
    thanks!
    (obviously all variables are generated prier to this code.)