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

Rigidbody2D doesn't move to the left

Discussion in 'Scripting' started by xred13, Dec 31, 2018.

  1. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    74
    This is 2D
    I have a script spawning bullets in position vector3.zero and the bullets are rotated and are given a speed to go towards the position of where the mouse was when we clicked.

    Code (CSharp):
    1.     public float speed;
    2.  
    3.  
    4.     Rigidbody2D rb;
    5.  
    6.     // Start is called before the first frame update
    7.     void Awake()
    8.     {
    9.         rb = GetComponent<Rigidbody2D>();
    10.     }
    11.  
    12.  
    13.     public void SetDirection(Vector3 savedMousePos)
    14.     {
    15.         float degrees = Vector3.SignedAngle(new Vector3(1,0,0), savedMousePos, Vector3.forward);
    16.         transform.Rotate(new Vector3(0,0,1),degrees);
    17.         Vector3 rbVelocity = savedMousePos.normalized * speed;
    18.         rb.velocity = rbVelocity;
    19.  
    20.         Debug.Log("Degrees: " + degrees);
    21.         Debug.Log("SavedMousePosition: " + savedMousePos);
    22.         Debug.Log("RB VELOCITY: "+rbVelocity);
    23.  
    24.     }
    where savedMousePos is the position of where we clicked with the mouse

    all the debug log values are correct, the script works fine if i shoot to the top, bottom or anywhere in the right, but the bullets wont go to the left.. any help why this happens? video:

     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Spin your camera around and see if its moving on the wrong plane.
    There is clearly one moving slightly left, perhaps they are colliding with somehting (the player) when they are shot
     
  3. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    74
    thanks.. yes it was colliding with the player!