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

Global Rotation

Discussion in 'Scripting' started by DasNeo, Jan 9, 2016.

  1. DasNeo

    DasNeo

    Joined:
    Jan 9, 2016
    Posts:
    2
    Hello,

    after a few hours searching for a solution I've decided to post my problem here.

    So I have a Cube and a Camera. The Camera is following and looking at the cube (It rotates with Mouse X Axis around the cube). Now I want the Cube to get the same y-Rotation as the camera. I did it like that:

    Code (CSharp):
    1. Quaternion newPos = transform.rotation;
    2. newPos.x = .0f;
    3. newPos.z = .0f;
    4. player.transform.rotation = newPos;
    The problem is that this rotation is only local. How do I change that? If I use player.tranform.Rotate and use Space.World I get the same results.


    Thanks!
     
  2. specterdragon

    specterdragon

    Joined:
    Dec 30, 2013
    Posts:
    21
    I'd like to clarify what you mean by world vs. local rotation. Local rotation is essentially orientation of the object on its own axis while world rotation results in translating the object to a different location. (Imagine an object at 1,0 that's rotated 90 degrees around the world origin. The object ends up at 0,1.)

    As I understand it, you're wanting the cube to face the same direction as the camera, correct? Could you simply set the rotations to be the same?
    Code (csharp):
    1. player.transform.rotation = camera.rotation
    In the event that you only want the Y (yaw) value modified, you can use something like this.
    Code (csharp):
    1. player.transform.rotation = Quaternion.LookRotation(new Vector3(camera.rotation.x, 0, camera.rotation.z));
    If this isn't what your looking for, please give a bit more about how the above is different than what you're after.
     
  3. DasNeo

    DasNeo

    Joined:
    Jan 9, 2016
    Posts:
    2
    Hello,

    I just found my Mistake. I messed something up there in my brain. The problem was not the rotation, it was my movement script. The Cube does rotate exactly like he should but I told the Cube to always hold on to the Global Axis.

    Thanks for your help!
     
  4. specterdragon

    specterdragon

    Joined:
    Dec 30, 2013
    Posts:
    21
    Glad you found the answer. Can't say I've never done something like that too. It's easy to get the various coordinate systems mixed up - even if it's just for a moment. ;)