- Home /
Camera-based movement is acting wonky
So, in short, I have a Cinemachine camera that follows my player around and which I can control the rotation of with my controller. I recently tried to add onto my script so that when the character moves with the joystick, it is relative to the direction the camera is pointing, instead of just the world axis. I watched several videos with similar methods, and I can't find anything I've done wrong with my code (Though judging by the results I've gotten, I definitely have somewhere), but for some reason when the player character moves, instead of the character's "forward" changing with the camera's "forward", the character's movement axis just flips from forward to backwards when the camera's y rotation goes from a positive number to a negative number.
Here is my code for my input and character movement:
using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel.Design; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Threading; using UnityEngine; using UnityEngine.InputSystem;
public class Ednacont : MonoBehaviour { PlayerControls controls;
public Camera m_MainCamera;
public CharacterController controller;
public float speed = 6f;
public float rotationspeed = 180f;
Vector2 move;
Quaternion targetRotation;
// Start is called before the first frame update
void Awake()
{
controls = new PlayerControls();
controls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
controls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
}
void Update()
{
Vector3 camfwd = m_MainCamera.transform.forward;
Vector3 camright = m_MainCamera.transform.right;
camfwd.y = 0;
camright.y = 0;
camfwd.Normalize();
camright.Normalize();
Vector3 direction = new Vector3(-camfwd.x * -move.y, 0, camright.z * move.x).normalized;
var input = new Vector3(camright.z * move.x, 0, -camfwd.x * move.y);
if (input != Vector3.zero)
{
targetRotation = Quaternion.LookRotation(input);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationspeed * Time.deltaTime);
if (direction.magnitude >= 0.1f)
{
controller.Move(direction * speed * Time.deltaTime);
}
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
As I said, everything works perfectly except for the fact that for some reason the player's rotation with the camera is "all or nothing" in a sense. The player moves well, and rotates to face the direction they are moving perfectly, but the axis of their movement is strangely limited. If the camera's y rotation is positive, they move along the world axis as normal, and if it is negative, it flips and they move on what seems to be a negative version of the world axis, but nothing in between. The few things I can think of about my project that are different than the videos and examples I've seen is that I am using a character controller to move my character instead of transform.translate, and my camera is controlled by Cinemachine. I am not sure how that would affect it, but I know that my camera's transform rotates properly so I don't think that should be a problem. I haven't found anybody else on here who has had this exact problem, so I figured I'd post this one. Please let me know if you require more information, this was a little hard for me to describe, thank you!