I am trying to rotate a game object toward a location (at a constant angular speed) and an having an issue with Quaternion.RotateToward. It seems to not do anything.
new_rot = Quaternion.RotateTowards(cur_rot,goal_rot,maxDeg);
cur_rot and goal_rot are different quaternions yet new_rot always is exactly equal to cur_rot, no matter the value of maxDeg. Why is this?
Ok this is what I am actually trying to do.
public static bool RotateToLoc(out Quaternion new_rot,
Vector3 goal_loc,
Vector3 cur_loc,
Quaternion cur_rot,
Vector3 cur_forward,
float speed, float elapsed)
{
Vector3 waypoint_pos = goal_loc;
waypoint_pos.y = cur_loc.y;
Vector3 waypoint_dir = waypoint_pos - cur_loc;
//float waypoint_dis = Vector3.Magnitude(waypoint_dir);
waypoint_dir.Normalize();
float angle = Vector3.Angle(cur_forward,waypoint_dir);
float delta_angle = elapsed * speed;
Quaternion goal_rot = Quaternion.LookRotation(waypoint_dir,new Vector3(0.0f,1.0f,0.0f));
new_rot = Quaternion.RotateTowards(cur_rot,goal_rot,delta_angle);
return true;
}
Ugh a terrible code posting
public static bool RotateToLoc(out Quaternion new_rot,
Vector3 goal_loc,
Vector3 cur_loc,
Quaternion cur_rot,
Vector3 cur_forward,
float speed, float elapsed)
{
Vector3 waypoint_pos = goal_loc;
waypoint_pos.y = cur_loc.y;
Vector3 waypoint_dir = waypoint_pos - cur_loc;
waypoint_dir.Normalize();
float angle = Vector3.Angle(cur_forward,waypoint_dir);
float delta_angle = elapsed * speed;
Quaternion goal_rot = Quaternion.LookRotation(waypoint_dir,new Vector3(0.0f,1.0f,0.0f));
new_rot = Quaternion.RotateTowards(cur_rot,goal_rot,delta_angle);
return true;
}
it doesn’t look like you’re assigning the quaternion to your object at any point.
I do after the return of this.
Quaternion.RotateTowards uses the Quaternion.Slerp method. This is is a smooth in/smooth out method. Constant speed cannot be derived using this method, but must use the Quaterion.Lerp method.
heh, odd, you are calculating angle, but not using it.
Also, just checking in your math here is a simpler way to do some of this:
Vector3 waypoint_dir = waypoint_pos - cur_loc;
waypoint_dir.Normalize();
Quaternion goal_rot = Quaternion.LookRotation(waypoint_dir);
float angularSpeed=Quaternion.Angle(cur_rot, goal_rot)/speed * Time.deltaTime;
new_rot = Quaternion.Lerp(cur_rot, goal_rot, angularSpeed);
it may take some playing with, but I think its a decent direction to go.