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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Freeze gameobject rotations in only 2 axes

Discussion in 'Scripting' started by Myrmidou, Dec 10, 2015.

  1. Myrmidou

    Myrmidou

    Joined:
    Jun 7, 2014
    Posts:
    30
    Hello everyone!

    I have a problem for quite a while now. I'm working with google cardboard and i'm trying to do an icon to change the viewmode from mono to stereo and vice versa when we look at it. The icon, which is under the player, needs to follow the player's moves in x & z and rotations, only in y.

    How can I manage to do this?

    I tried to make the icon a child of the player gameobject and freeze rotations with script but nothing worked.
     
  2. troyfury

    troyfury

    Joined:
    May 15, 2015
    Posts:
    19
    Maybe attached to the icon, where targetTransform is the player, the icon will copy the x&z position of the target and the y rotation only:

    public Transform targetTransform;

    void Update(){
    transform.position = new Vector3(targetTransform.position.x, transform.position.y, targetTransform.position.z);
    transform.eulerAngles = new Vector3(transform.eulerAngles.x ,targetTransform.eulerAngles.y, transform.eulerAngles.z);
    }