Rotating single axis like the same axis of an other gameobject
In my game i'm trying to rotate the x-axis of the head of the player like the x-axis of the camera in it, but i don't know how to do that. I know how to rotate single axes, but not how to rotate them like an other gameobjects axis.
Here`s my code, which doesn't work, because there it is rotating all the axes:
using UnityEngine; using System.Collections;
public class Kopf : MonoBehaviour {
public GameObject MainCamera;
public GameObject Camera2;
void Update () {
if (MainCamera.activeInHierarchy)
{
transform.rotation = MainCamera.transform.rotation;
}
if(Camera2.activeInHierarchy)
{
transform.rotation = Camera2.transform.rotation;
}
}
}
Comment
If you only want the x-Rotation to be the same, you could do:
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles.x = Camera2.transform.rotation.eulerAngles.x;
transform.rotation.eulerAngles = eulerAngles;
Hope this helps my Freund :)
Or use this snippet which looks a bit cleaner :)
Quaternion rot = transform.rotation;
rot.eulerAngles.x = Camera2.rotation.eulerAngles.x;
transform.rotation = rot;
Your answer
Follow this Question
Related Questions
when trying to rotate, the object transforming itself 0 Answers
Why this isn't working? 1 Answer
Cannot properly access player position 1 Answer