- Home /
How to rotate a object to the rotation of another object?
Hey, I need to make an object(the player) to get the rotation of the Rail(a prefab object in game). The script I have now, is the player reacts onTrigger(the rail is the trigger). In the rail this is the code: public Vector3 ChangeRotation = new Vector3();
And in the start:
ChangeRotation = transform.rotation.eulerAngles;
And in the player:
public class PlayerMove : MonoBehaviour {
public GameObject Rails;
public GiveRotation getrotation;
public Vector3 NewRotation = new Vector3();
// Use this for initialization
void Start () {
getrotation = Rails.GetComponent();
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.forward*Time.deltaTime);
}
void OnTriggerEnter(){
Debug.Log("HEllo");
NewRotation = getrotation.ChangeRotation;
transform.Rotate(NewRotation, Space.Self);
//Quaternion.RotateTowards(transform.position,NewRotation,90);
}
}
However this just makes the player move strange.
Answer by whydoidoit · Apr 22, 2013 at 11:04 AM
You need to just set the rotation, not try to rotate the object by a number of degrees:
transform.eulerAngles = getrotation.ChangeRotation;
Thanks for the quick anwser. After a few tests it worked!
Your answer
![](https://koobas.hobune.stream/wayback/20220613100815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How to always rotate clockwise 1 Answer
Making a rotating object rotate with a parent object 1 Answer
Images Rotation 1 Answer
How to rotate about 360 on a coroutine 3 Answers