Answer the question
In order to leave comments, you need to log in
How to rotate an object without affecting the y-axis?
I need to make a smooth rotation of the object, I do this
void LookAtTarget(GameObject gameObject){
if(gameObject != null){
Vector3 direction = target - gameObject.transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Speed * Time.deltaTime);
}
}
Answer the question
In order to leave comments, you need to log in
I have two suggestions how to solve it:
1. Freeze the Y rotation in the inspector or through the code, then, if I'm not mistaken, Y will not change.
2. Create a float variable that will be equal to the rotation in y that is now (what you need) and in the method after the rotation, return the rotation in y to the value that was before the rotation. Something like this:
float stay_y;
void Start()[
stay_y = transform.rotation.y; //Берешь значения y до поворота
}
void LookAtTarget(GameObject gameObject){
if(gameObject != null){
Vector3 direction = target - gameObject.transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Speed * Time.deltaTime);
transform.rotation = new Quaternion(transform.rotation.x, stay_y, transform.rotation.z);//Возвращаешь rotation по y на значение до поворота.
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question