2d forward

I’m working on a simple script to create a flying AI that ignores obstacles and blindly pursues the player. It currently looks at the player with a slight offset in it’s Z rotation. It also slowly rotates around it’s Z axis when the player moves from one side of it to the other allowing it to flip around. Because it’s Z rotation is never 0 I can’t use Vector3.forward, is there a 2d version of it or some way to lock the Z position/rotation of a 2d rigidbody?

If I understand what you are saying is you want a 2d sprite that moves to the player based on the rotation of said sprite.

if that is the case what I would do is something like this

//velocity is the direction you want the object to go multiplied by its speed

Vector3 dirX  = gameObject.transform.right * velocity.x;
Vector3 dirY = gameObject.transform.up * velocity.y;

rigidbody2D.AddForce(dirX);
rigidbody2D.AddForce(dirY);

hope this helps.

Took me a lot of toying with it to fine tune it but that did the trick. Rather than applying force twice I chose to apply it once, but it’s still pretty much the same. Thank you, you’ve been a huge help SonicRaptor.

rigidbody2D.AddForce(new Vector2(dirX.x,dirY.y));
1 Like

ANSWER IS HERE

http://answers.unity3d.com/answers/613109/view.html

1 Like

I’d just like to point out that “gameObject.transform.right” can be simplified to “transform.right”.