Changing the Axis of a Copied Rotation

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.

6742747--777100--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”.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class LookAt : MonoBehaviour

{

    // This quaternion here is for the capsule to reference

    public static Quaternion rotationraw;


    // This public target is the cursor, placed manually in the Unity Editor

    public Transform target;


    void Update()

    {

        transform.right = target.position - transform.position;

        rotationraw = transform.rotation;

    }

}

Here is my extremely simple code for the capsule. It basically says “rotate like the joystick using that ‘rotationraw’ variable it’s broadcasting”.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class PlayerMovement : MonoBehaviour

{

    void Update()

    {

        transform.rotation = LookAt.rotationraw;

    }

}

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.

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 :smile:

transform.rotation = Quaternion.Euler(0, LookAt.rotationraw.eulerAngles.z, 0);
1 Like

Thank you, cat. It worked. One day I’ll actually take a math class and figure this out.

Don’t encourage him. All he did was sit there with a look of detached disdain as I typed that answer out.

1 Like