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

Changing the Axis of a Copied Rotation

Discussion in 'Scripting' started by camerontownsend, Jan 20, 2021.

  1. camerontownsend

    camerontownsend

    Joined:
    Mar 7, 2019
    Posts:
    14
    Hello coding masters. I’m new to coding and have run into what I’m sure is an extraordinarily simple problem- however it’s so specific that I haven’t been able to solve it with a simple google search.

    I’ve got two simple elements here in this scene: a capsule-like shape in 3D space, and a blue icon representing that capsule in 2D space.

    Extra.gif

    That 2d icon is like a joystick controlled with the mouse, and it’s supposed to control the capsule’s movement. So far it’s working perfectly except for one problem: The capsule is currently rotating on the Z axis, and I’d like it instead to rotate on the Y axis. That’s it.

    Here is my extremely simple code for the joystick icon- it basically says “rotate toward the cursor and broadcast your rotation as ‘rotationraw’ for the capsule to see”.
    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4.  
    5. using System.Collections.Generic;
    6.  
    7. using UnityEngine;
    8.  
    9.  
    10. public class LookAt : MonoBehaviour
    11.  
    12. {
    13.  
    14.     // This quaternion here is for the capsule to reference
    15.  
    16.     public static Quaternion rotationraw;
    17.  
    18.  
    19.     // This public target is the cursor, placed manually in the Unity Editor
    20.  
    21.     public Transform target;
    22.  
    23.  
    24.     void Update()
    25.  
    26.     {
    27.  
    28.         transform.right = target.position - transform.position;
    29.  
    30.         rotationraw = transform.rotation;
    31.  
    32.     }
    33.  
    34. }
    35.  
    36.  
    37.  
    Here is my extremely simple code for the capsule. It basically says “rotate like the joystick using that ‘rotationraw’ variable it’s broadcasting”.

    Code (CSharp):
    1.  
    2.  
    3.  
    4. using System.Collections;
    5.  
    6. using System.Collections.Generic;
    7.  
    8. using UnityEngine;
    9.  
    10.  
    11. public class PlayerMovement : MonoBehaviour
    12.  
    13. {
    14.  
    15.     void Update()
    16.  
    17.     {
    18.  
    19.         transform.rotation = LookAt.rotationraw;
    20.  
    21.     }
    22.  
    23. }
    24.  
    As you can hopefully see, the capsule is rotating just fine- I’d just like it to rotate left and right as opposed to up and down. I plan to use this to control the direction of the capsule as a rigid body in the very near future. Any and all help would be greatly appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Generally I stray away from eulerAngles, but your case is simple enough where this should just work, at least until the situation gets more complex :D
    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0, LookAt.rotationraw.eulerAngles.z, 0);
     
    camerontownsend likes this.
  3. camerontownsend

    camerontownsend

    Joined:
    Mar 7, 2019
    Posts:
    14
    Thank you, cat. It worked. One day I'll actually take a math class and figure this out.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Don't encourage him. All he did was sit there with a look of detached disdain as I typed that answer out.
     
    camerontownsend likes this.