Relative movement isn't changing based on camera orientation.
So I have this script setup for my third person movement on my player character, and earlier this morning it was working great. I didn't change anything but now my direction wont change based off of camera orientation. I am using a Cinemachine Free Roam Camera setup to the player object.
It should set "W" or "Forward" movement relative to the cameras direction but it currently isn't working anymore.
The section below should be the portion that handles this, but clearly I am missing something. For more reference the overall movement and jumping works perfectly fine, just changing the direction based on where the camera is facing isnt working.
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) Vector3.forward; controller.Move(moveDir.normalized speed * Time.deltaTime);
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
//Connectors for plaer and camera elements.
public CharacterController controller;
public Transform cam;
//variables for speed, jump, and gravity.
public float speed = 6;
public float gravity = -9.81f;
public float jumpHeight = 3;
Vector3 velocity;
bool isGrounded;
//GroundCheck variables
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
//Player movement variables and rotation based off local camera location.
float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;
// Update is called once per frame
void Update()
{
//Jumping, checks for terrain variable GroundCheck
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
//Sets grounded speed
if (controller.isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//Jump button Check
if (Input.GetButtonDown("Jump") && controller.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
//gravity settings
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//walking input
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
//Moving direction math, tells the movement to follow based off local camera rotation
if (direction.magnitude >= 0.1f)
{
//Simple math for movement and player rotation using directional angles.
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
//This tells the character to orient "forward" relative to the cameras current local rotation.
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
So update:
I changed nothing but adding a new scene, added the character prefab and readded the attached camera settings, and it worked perfectly fine. If anyone sees any problems with this, I would be much appreciated.
Your answer
Follow this Question
Related Questions
Help With Camera rotation on X and Y Axis (Target Focused) 1 Answer
Making the camera rotation in line with the ball 1 Answer
viewing on the Mouse Y axis not working. 0 Answers
,Third Person Movement Script Help 0 Answers
Using DampSmooth in EulerAngle is affecting the Position.Z value of the gameObject. 0 Answers