- Home /
Object isnt rotating when moving towards an object
So i want 2d plane to move towards another plane and rotate it, but its rotating only when object is null. Thanks.
void Update ()
{
transform.Rotate(0, 2, 0);
player = GameObject.FindGameObjectWithTag("Player");
if(player !=null)
{
transform.LookAt(player.transform.position,Vector3.forward);
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
Answer by dan19 · Jul 28, 2013 at 01:54 PM
This is because Transform.LookAt()
is quite different from Transform.Translate()
and Transform.Rotate()
.
Rotate()
and Translate()
are sort of incremental. They take the existing coordinate system and modify it a little bit.
LookAt()
, on the other hand, is not incremental at all, it is absolute. It completely disregards the previous rotation of the coordinate system, and sets it anew. So LookAt()
after Rotate()
completely destroys what Rotate()
did.
It depends on what exactly you want to get. One way is like this: don't call LookAt() in every Update(). Call it once when you want to start the movement (maybe in Start()).
You are right, but i need object always go to an anther object and rotate, maybe there is another way to do this?
You can also move your Rotate() method, and make the rotation not incremental (2 degrees per frame in your case), but absolute. Something along these lines:
transform.LookAt(player.transform.position, Vector3.forward);
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.Rotate(0.0f, Time.time * rotationSpeed, 0.0f);
Your answer
Follow this Question
Related Questions
Rotation with multiple objects 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotation using Unity2D 3 Answers
Why the player is not rotating and moving at the same time ? 0 Answers