,Third Person Movement Script Help
Hello everyone! I have been trying to get this to work for three days banging my head against the wall :( I have gotten it to work for the most part by piecing other people scripts and tutorials together. I would like my character to rotate in place when stationary and pressing any button on the horizontal axis. Also when I run forward and press horizontal axis would like character to gradually turn while running. Except if the rightmouse button is held down then strafe left and right if pressing the horizontal axis. This probably is confusing and I can draw it out if need be. Below is my current code and if anyone has a suggestion to get ride of the nasty if statement "if(horizontal != 0 || vertical != 0)" on line 72 that would be awesome. Thanks in advance for any help I appreciate it. I am also using cinemachine free look camera to control the camera for the player set to heading target forward and orbit world space. playermovementscript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class ThirdPersonMovement : NetworkBehaviour
{
public CharacterController controller;
public Animator animator;
public Transform cam;
public AudioListener myAudioListener;
InputActions inputActions;
// public floats editable in GUI
public float mouseSensitivity = 3f, playerSpeed = 50f;
public float jumpForce = 1f;
public float gravity = 10f;
public float turnSmoothTime = 0.1f;
public float turnSpeed = 8.0f;
private float rotSpeed = 80;
private float rot = 0f;
// private variables not editable in GUI
private float vertVelocity;
private Vector2 movementInput;
private Vector3 direction;
private Vector3 stationaryCheck;
private float turnSmoothVelocity;
private bool hasJumped;
private bool rightMouseInput = false;
void Awake()
{
inputActions = new InputActions();
inputActions.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
inputActions.Player.Camera.performed += ctx => rightMouseInput = ctx.ReadValueAsButton();
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
// exit from update if this is not the local player
return;
}
PlayerMovement();
if(inputActions.Player.Jump.triggered)
{
hasJumped = true;
}
ApplyGravity();
}
void PlayerMovement()
{
// set variables for inputActions move performed (WASD Keys)
float horizontal = movementInput.x;
float vertical = movementInput.y;
direction = new Vector3(horizontal, vertVelocity, vertical);
// finds the angle of where camera is pointing and smooth turn
float targetAngle = Mathf.Atan2(0, 0) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
if(horizontal != 0 || vertical != 0)
{
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
direction = Quaternion.Euler(0f, targetAngle, 0f) * direction;
controller.Move(direction * playerSpeed * Time.deltaTime);
if(vertical > 0 && controller.isGrounded)
{
animator.SetFloat("Walk", 1);
}else{animator.SetFloat("Walk", 0);}
}
void ApplyGravity()
{
// applies a consistent 9.1f gravity if
if (controller.isGrounded == true)
{
if(hasJumped == false){
vertVelocity = Physics.gravity.y;
}else{
vertVelocity = jumpForce;
}
}else{
// consistenly increase speed falls
vertVelocity += Physics.gravity.y * Time.deltaTime;
vertVelocity = Mathf.Clamp(vertVelocity, -50f, jumpForce);
hasJumped = false;
animator.SetFloat("Walk", 0);
}
}
void AnimationControls()
{
}
void OnEnable() {
inputActions.Enable();
}
void OnDisable() {
inputActions.Disable();
}
}
Your answer
Follow this Question
Related Questions
Locking Camera's rotation on a rolling character 0 Answers
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
How to have your player controls change while your camera rotates? 0 Answers