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.
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.