- Home /
Rotating without slowing down before reaching target
// Working on this for a few days now and I couldn’t find anything on the net that works. I need to turn //a character to a target object in the scene without slowing down at the end of the rotation. When I use //lerp the character works fine rotating in the counterclockwise direction, but never gets close enough //to the target turning in the clockwise direction (I think that is because of slowing down).
float diff = transform.rotation.eulerAngles.y - couchTargetFloorHeight.eulerAngles.y;
if (Mathf.Abs(diff) > enoughTurning)
{
Vector3 direction = couchTargetFloorHeight.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
}
Answer by MarioSantoso · Apr 19, 2018 at 12:29 PM
You can try RotateTowards
Thank you for your answer. Is there any way to rotate on the y axis? $$anonymous$$y character is rotating on the z axis. public float speed; private void RoomCenterTurn() { float diff = transform.rotation.eulerAngles.y - couchTargetFloorHeight.eulerAngles.y;
if ($$anonymous$$athf.Abs(diff) > enoughTurning)
{
float step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, couchTargetFloorHeight.rotation, step);
}
Oh sorry, i was't aware there's a follow up question. Anyway, glad it worked out for you.
Thank you very much for your help. Here is the working code in case someone needs it:
public float speed;
Vector3 eulerAngles;
private void RoomCenterTurn()
{
//Vector3 direction = couchTargetFloorHeight.position - transform.position;
//Quaternion rotation = Quaternion.LookRotation(direction);
//transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
float diff = transform.rotation.eulerAngles.y - couchTargetFloorHeight.eulerAngles.y;
Debug.Log("couch x " + couchTargetFloorHeight.eulerAngles.x);
if ($$anonymous$$athf.Abs(diff) > enoughTurning)
{
//transform.LookAt(transform.position - couchTargetFloorHeight.position);
eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(0, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
float step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, couchTargetFloorHeight.rotation, step);
Debug.Log("difference " + $$anonymous$$ath.Abs(diff));
}
Answer by Gigaman1 · Apr 19, 2018 at 06:22 PM
Thank you for your answer. I’ve looked at lookat before but I didn’t remember why I couldn’t make it work. The code below starts my character aligned with my camera. Do you have any hints on how I could fix it? private void RoomCenterTurn() { float diff = transform.rotation.eulerAngles.y - couchTargetFloorHeight.eulerAngles.y;
if (Mathf.Abs(diff) > enoughTurning)
{
transform.LookAt(transform.position - couchTargetFloorHeight.position);
}
Thank you very much for your help! Here is the working code in case someone needs it: public float speed; Vector3 eulerAngles; private void RoomCenterTurn() { //Vector3 direction = couchTargetFloorHeight.position - transform.position; //Quaternion rotation = Quaternion.LookRotation(direction); //transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime); float diff = transform.rotation.eulerAngles.y - couchTargetFloorHeight.eulerAngles.y; Debug.Log("couch x " + couchTargetFloorHeight.eulerAngles.x);
if ($$anonymous$$athf.Abs(diff) > enoughTurning)
{
//transform.LookAt(transform.position - couchTargetFloorHeight.position);
eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(0, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
float step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, couchTargetFloorHeight.rotation, step);
Debug.Log("difference " + $$anonymous$$ath.Abs(diff));
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Rotate my Object with Mouse and Touch 0 Answers
[SOLVED] Why is text not appearing? 2 Answers
script type variables 1 Answer