- Home /
How do I move the player towards the direction of the camera using cinemachine?
Hi, Im new to coding and trying to get the CM vcam to work with my script so the player moves in the direction of the camera (in first person). How might I go about making it work with what I have going now? (Rotation and Look) section near the bottem.
public class PlayerController : MonoBehaviour
{
//Refernce to Inputs / Charactercontroller Component on body
private PlayerInputs playerInput;
private CharacterController characterController;
//Player Camera
private Transform camTransform;
private Vector2 lookDirection;
private float rotationFactorPerFrame = 1.0f;
//test
private Vector3 startingRotation;
private float clampAngle = 80f;
private float horizontalSpeed = 10f;
private float verticalSpeed = 10f;
//Movement
private Vector2 currentMovementInput;
private Vector3 currentMovement;
[Header("Movement")]
public float movementSpeed;
private bool isMovementPressed;
//Gravity is a harness...
private float gravity = -3f;
private float groundedGravity;
//Jumping
private bool isJumpPressed;
private float initialJumpVelocity = 1.5f;
private float maxJumpHeight = 4.0f;
private bool isJumping = false;
//Activating the Input system
private void Awake()
{
playerInput = new PlayerInputs();
characterController = GetComponent<CharacterController>();
playerInput.CharacterControls.Move.started += OnMovementInput;
playerInput.CharacterControls.Move.performed += OnMovementInput;
playerInput.CharacterControls.Move.canceled += OnMovementInput;
playerInput.CharacterControls.Jump.started += OnJump;
playerInput.CharacterControls.Jump.canceled += OnJump;
playerInput.CharacterControls.Look.started += OnLook;
playerInput.CharacterControls.Look.performed += OnLook;
playerInput.CharacterControls.Look.canceled += OnLook;
}
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
//For constant Moveing Attributes
private void Update()
{
characterController.Move(currentMovement * Time.deltaTime * movementSpeed);
GravityHandler();
JumpHandler();
}
//Gravity Listener
private void GravityHandler()
{
bool isFalling = currentMovement.y <= 0.0f || !isJumpPressed;
float fallMultiplier = 2.0f;
if (characterController.isGrounded)
{
float groundedGravity = -0.05f;
currentMovement.y = groundedGravity;
}
else if (isFalling)
{
float previousYVelocity = currentMovement.y;
float newYVelocity = currentMovement.y + (gravity * fallMultiplier * Time.deltaTime);
float nextYVelocity = Mathf.Max((previousYVelocity + newYVelocity) * 0.5f, -20.0f);
currentMovement.y = nextYVelocity;
}
else
{
float previousYVelocity = currentMovement.y;
float newYVelocity = currentMovement.y + (gravity * Time.deltaTime);
float nextYVelocity = (previousYVelocity + newYVelocity) * 0.5f;
currentMovement.y = nextYVelocity;
}
}
//Rotation & Look
private void OnLook(InputAction.CallbackContext context)
{
if (startingRotation == null) startingRotation = transform.localRotation.eulerAngles;
Vector2 deltaInput = GetMouseDelta();
startingRotation.x += deltaInput.x * verticalSpeed * Time.deltaTime;
startingRotation.y += deltaInput.y * horizontalSpeed * Time.deltaTime;
startingRotation.y = Mathf.Clamp(startingRotation.y, -clampAngle, clampAngle);
state.RawOrientation = Quaternion.Euler(-startingRotation.y, startingRotation.x, 0f);
}
private static Vector2 GetMouseDelta()
{
playerInput.CharacterControls.Look.ReadValue<Vector2>;
}
Comment