Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotate object based on camera rotation

Discussion in 'Scripting' started by blabberbytes, Apr 20, 2018.

  1. blabberbytes

    blabberbytes

    Joined:
    Nov 22, 2014
    Posts:
    13
    Im building this game that requires the rotation of an object relative to the camera in 3D space. Essentially, i have an object that is the child of an FPS camera which needs to be rotate in front of the camera acting as a third person character. I am developing in VR so mouseX, MouseY wont work. Help is greatly appreciated!
     
  2. blabberbytes

    blabberbytes

    Joined:
    Nov 22, 2014
    Posts:
    13
    So far I have come up with this but it doesnt work:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class HeadMotion : MonoBehaviour {
    5.      public GameObject headRotationRoot;
    6.      public Quaternion rotationFromTheHead;
    7.      public Transform tempTransform;
    8.      void Start () {
    9.      }
    10.      // Update is called once per frame
    11.      void Update () {
    12.          tempTransform = headRotationRoot.transform;
    13.          tempTransform.localRotation = headRotationRoot.transform.localRotation;
    14.          transform.localRotation = tempTransform.localRotation;
    15.          Debug.Log ("headRotationRoot: " + headRotationRoot.transform.localRotation.x );
    16.          Debug.Log ("tempTransform: " + tempTransform.localRotation.x );
    17.          Debug.Log ("transform: " + transform.rotation.x);
    18.      }
    19. }