- Home /
Side scroller - rotate object towards player.
I am working on a side-scroller - movement is restriced to the x and y planes. I've looked in several places on different forums and can't find a good solution - I have a cylinder-like object that I want to point at the player at all times. I've tried several things but nothing keeps movement/rotation to the x and y planes. Any help is greatly appreciated.
Answer by zero3growlithe · Nov 13, 2011 at 11:05 AM
Try using this script:
var Player : Transform; // probably assign this in editor
function Update(){
var point: Vector3 = Player.position
point.z = 0.0;
transform.LookAt (point);
}
And here's an explanation on how "transform.LookAt" works: http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html
Thanks! - that worked great. One more question - would you know how to make it smoothly rotate ins$$anonymous$$d of constantly pointing at the player (while keeping rotation in one axis)?
As in, point towards the player eventually, but still rotate more slowly?
var targetLocation = Quaternion.LookRotation(point);
transform.rotation = Quaternion.Slerp(transform.rotation, point, Time.deltaTime * 5);
the 5 can be increased or decreased as you wish for more / less smoothing. (not necessarily in that order)
Doesn't seem to work. The first two parameters for Quaternion.Slerp must be rotations - "point" isn't a rotation.
Check out the standard assets: Camera Scripts > SmoothLookAt
Your answer
Follow this Question
Related Questions
Why does my OnCollisionEnter2D not work? 3 Answers
how do i make an object always face the player? 5 Answers
2D jump with Raycast 1 Answer
Start Coroutine after other has finished 4 Answers