- Home /
Object moving sideways when translating using forward vector.
I'm trying to move towards a point and I'm currently calling two functions during every update.
void moveForward()
{
transform.Translate(transform.forward*speed*velocity);
}
void turnTowards()
{
Vector3 desired = point -transform.position;
Vector3 rotation = Vector3.RotateTowards(transform.forward,desired,turnSpeed*Time.deltaTime,1);
transform.rotation = Quaternion.LookRotation(rotation);
}
The object rotates as I want but doesn't move correctly, using only the rotate function points the object at the target. It is as if the transform.forward vector is wrong. The objects gets to the target point but the it looks wrong. I call rotate before movement.
I would expect the object to rotate a bit, move a bit forward in the new direction, rotate, move, rotate, move, etc...
It looks like of move and forward uses two different forward vectors which slowly syncs, only way I can describe the way it looks.
Answer by robertbu · Mar 05, 2013 at 06:59 PM
If 'velocit'y is a vector, it will change forward. Forward is the side of your model facing positive 'z' when there is no rotation (0,0,0) Your turnTowards using transform.forward is not code I've seen before and I think it is not following the arc you want it to. Here is how I typically see rotation over time coded:
void turnTowards()
{
Vector3 desired = point - transform.position;
Quaternion q = Quaternion.LookRotation(desired);
transform.rotation = Quaternion.RotateTowards (transform.rotation, q, turnSpeed*Time.deltaTime);
}
$$anonymous$$y model is rotated so it's aligned with the forward vector but I still get the same sideways sliding with your rotation code.
Can you describe what you want? Are you looking to move directly to the goal while rotating? Here is code that moves towards the goal in a line (though unlike yours, it stops at the goal):
void moveForward()
{
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, point, speed * Time.deltaTime);
}
I want my object to turn and moce towards the target, in this case my object is a boat. I dont want it to just turn on the spot but move while it turns but as I've said with everything I've tried I just get the same behavior.
But I figured out a way to move forward. I tried to update position by either translating with the direction vector or adding the direction vector too the position. As you said forward would be the models z axis but I moved along the x axis by doing this, by using translate(0,0,speed*Time.delta) I got forward motion. Thanks for the help!