Search Unity

Movement in forward direction

Discussion in 'Scripting' started by Zalosath, Jun 17, 2019.

  1. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    2D Top down, turrets fire bullets, the bullets are supposed to move according to their transforms right direction
    (transform.right)

    But they don't really stick to this rule; I think it may be to do with the sprite that I'm using, it's a long thin blue strip.

    Here's an example of the kind of behaviour I'm getting:
    https://gyazo.com/ed8acf2fbcd27a4a684cc87e0e68ce72

    Despite not being the clearest GIF, I think it demonstrates the point, the object moves fine when it is at 0 degrees.

    But any rotation makes it offset, I've tried adjusting the sprite so that the pivot is closer to the bottom, and also where it is closer to the top, I tried making the sprite a square so that rotation would be uniform, but no luck unfortunately.
    The code I use for moving forward is:
    this.transform.Translate (transform.right * Time.deltaTime * speed);

    I also tested the object with different parents; ones with rotation, ones without rotation, and even just no parent at all, but unfortunately this is still not working.

    When the object is instantiated it receives the rotation of the object that fired it, which in my case is a turret. The rotation that it gives is correct, but the direction that it travels is not.

    EDIT: Here's another GIF that I think captures what I mean a little better.
    https://gyazo.com/a1ccf313df20bcbefeb6d36f7165e128

    Thanks.
     
    Last edited: Jun 17, 2019
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    By default, transform.Translate moves an object relative to its local space. So, don't use transform.right, use Vector3.right. Alternatively, call
    transform.Translate(transform.right * SPEED, Space.World);
     
  3. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    Works great. That's a bunch.

    Everything I googled online told me the opposite, suppose I was reading everything wrongly.