- Home /
Look at rotation at a moving object while moving (C#)(2D)
I used some javascript that happened to work in C#.
var newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.back);
newRotation.x = 0.0f;
newRotation.y = 0.0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);
It's attached to an enemy space ship that follows you by turning to face you and moving forward. It works fine if the enemy is stationary and only the player moves, but when they both move the rotation jitters and doesn't work properly at all. If I turn the rotation speed up to the point of instantaneous, then it works fine, but in this game I need it to be a slow and smooth rotation.
Any Idea on how to fix this, or maybe an alternative way of rotating on a 2D axis would be awesome.
Answer by AngryGenius · Jan 23, 2014 at 10:50 PM
One problem is that you're editing individual components of a quaternion, which is really complicated, so you should probably convert the above code to something more like
Quaternion newRotation = Quaternion.LookRotation(transform.position - target.position, Vector3.back);
Vector3 eulers = newRotation.eulerAngles;
eulers.x = 0.0f;
eulers.y = 0.0f;
nwRotation = Quaternion.Euler(eulers);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);
And then move that code to LateUpdate or FixedUpdate if that doesn't fix it.
Right now they will only look left and right. If I'm on the left side, they just snap and face straight to the left with no in between. Late update didn't make a difference. I also changed nwRotation to newRotation because it looks like a typo.
Answer by venhip · Jan 23, 2014 at 11:39 PM
Hey W1k3,
I'm working on a 2D game at the moment and this is the bit in my scripts I use for looking at another object:
rigidbody2D.transform.eulerAngles = new Vector3(0,0,Mathf.Atan2((TARGETTRANSFORM.position.y - transform.position.y), (TARGETTRANSFORM.position.x - transform.position.x))*Mathf.Rad2Deg - 90);
I adapted this from a script that causes the object the script is on to look at the mouse. If this doesn't work let me know and I'll try to provide a better explanation, but this should work if you replace "TARGETTRANSFORM" with a transform of whatever name you want.
I program in C# but when I tested this with Javascript it didn't throw any errors, so I hope it's fine for you.
Good luck.
P.S. Put this in your FixedUpdate function or wherever your previous rotation script was.
Thanks for the Reply. This rotates to face my player well, but do you know if there is a way to modify the speed in which it rotates? Also, if the enemy get's hit by a projectile, it causes it to rotate a tiny bit from the force. After that the enemy doesn't face the player right on, always a bit to the left or the right. After a while it begins to face the player again, but it's a bit of nuisance. I'm using C# by the way.
Answer by robertbu · Jan 23, 2014 at 11:41 PM
Try this instead:
var dir = transform.position - target.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
var newRotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotatespeed);
This assumes that this object is a sprite or Quad in which the 'forward' is to the right. If the forward is 'up', then you will need to adjust the angle by 90 degrees after the Atan2() call.
Your answer
Follow this Question
Related Questions
Get slerp to work just as LookAt(,Vector3.right) does 1 Answer
How can I get the rotation between two objects 1 Answer
Gameobject Rotation Calculation 0 Answers
Interpolate rotation of character. 0 Answers
Quaternion.Slerp 1 Answer