I’m writing a script to make my player’s y rotation = my camera’s y rotation, but leave the player’s x and z rotation the same. The player is a rolling ball, so I can’t fix the x and z rotations, and I’m using a 3d Cinemachine camera that I need to be able to rotate in all directions as well. This is my current script:
…but that gave me an error involving Quaternions, which I’ve yet to have a 100% solid understanding of. Any help is appreciated, especially if it results in progress, and if more info is needed, I’ll gladly provide.
Based on our earlier chat, I think you mean you want the camera to affect how the inputs rotate based on which way you’re facing, right?
If you want to see an example of what I spoke of in the other thread, where you take normal “raw” input and rotate it to match the camera, check this file, like line 124 to line 128 or so.
@Kurt-Dekker I tried the code you linked, and I’ve been given the error “type of name or namespace name ‘_____’ could not be found” for two instances of ‘VAButton’, and one instance of ‘IPushable’… I assume I’m missing something here?
Also @Kurt-Dekker , I managed to work out all of my other issues independently, many thanks to your previous help! I thought I solved this problem as well, but soon realized that the ball didn’t really rotate anymore, since all rotations were made to match the camera.
I figured out how I am going to make the player move relative to its Y rotation, all I need is a way to make the Y rotation of the player and the camera match, without affecting the players other axis rotations.
You need to use eulerAngles. Quaternions use complex math and a quaternion’s x, y, and z values are not what is seen in the editor. The editor shows eulerAngles.
After some serious trial and error and manual reading, I’ve found a solution, which I’ll attach for the sake of anyone with a related problem.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody player;
public Camera camera;
public float maxSpeedInput;
void FixedUpdate()
{
GetComponent<Rigidbody>().maxAngularVelocity = Mathf.Infinity;
float torque = maxSpeedInput;
float speed = GetComponent<Rigidbody>().velocity.magnitude;
Rigidbody rb = GetComponent<Rigidbody>();
Vector3 cameraForward = Vector3.Cross(camera.transform.right, Vector3.up);
rb.AddTorque(cameraForward * -Input.GetAxis("Horizontal") * torque * Time.deltaTime);
rb.AddTorque(camera.transform.right * Input.GetAxis("Vertical") * torque * Time.deltaTime);
}
}
I used a Cinemachine FreeLook Camera to avoid having to write unnecessary code, and the settings are pretty straightforward too! I’m sure there’s some further simplifying that can be done, but it will suffice for me. Thanks those that aided me in achieving this milestone (definitely a newb, so this is a huge achievement for me).