How to make changing direction in my FPS game less jerky?
I'm currently developing the movement script of my first FPS game but the movement still seems a little bit off mainly because when I change directions (for example when going forward and then pressing the s key to go back) the change in velocity is instantaneous. Any way to gradually decelerate and then change directions possibly even accelerate in the new direction? Thanks!
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using UnityEditorInternal;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Input
PlayerControls controls;
Vector2 move;
private CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 5f;
private Vector3 velocity;
private Transform groundChecker;
public float groundDistance = 0.2f;
public LayerMask groundMask;
bool isGrounded;
private void Awake()
{
controls = new PlayerControls();
controls.Player.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
controls.Player.Move.canceled += ctx => move = Vector2.zero;
controls.Player.Jump.performed += ctx => Jump();
}
private void Start()
{
controller = GetComponent<CharacterController>();
groundChecker = transform.GetChild(0);
}
private void Update()
{
isGrounded = Physics.CheckSphere(groundChecker.position, groundDistance, groundMask, QueryTriggerInteraction.Ignore);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
Vector3 moveDirection = transform.right * move.x + transform.forward * move.y;
controller.Move(moveDirection * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void Jump()
{
if (isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
private void OnEnable()
{
controls.Player.Enable();
}
private void OnDisable()
{
controls.Player.Disable();
}
}
Answer by BenWiller1989 · Sep 04, 2020 at 11:02 AM
It seems so weird. Did you ever use Mathf.Lerp? Use it for your forward direction and yes, do use fixed Update. You might not realize the difference yet, but trust me, it'll catch up.
Yes!! Although it wasn't $$anonymous$$athf.Lerp but Vector3.Lerp that worked, it worked. I didn't make clear I was a unity noob but thankfuly searching for $$anonymous$$ath.Lerp lead me to Vector3.Lerp quite quickly. Just one follow up question: Should I put all the movementDirection and velocity calculations in FixedUpdate too, or just the controller.$$anonymous$$ove statement?
Anyways, thanks a bunch!
Yeah, I still use $$anonymous$$athf.Lerp for my velocity speed and multiply it with the loystick input and some other factors and move the character Controller after all, it works perfectly. But you can use, whatever fits the your needs. Linear interpolation is very useful. You have also color.Lerp and stuff like that. If you need it hard coded, cause Lerp does only use 0 and 1 for y1 and y2, then you can use x = (x2+((x1-x2) /(y1-y2)) *(y-y2)). For your question: I'd recommend, you put everything in the fixedUpdate, that's needs to be calculated frequently, like inputs, constantly updating calculations and things like that. But that's yours to decide, what needs to be updated.
Answer by Reid_Taylor · Sep 04, 2020 at 02:06 AM
Try using Fixed Update instead of Update
Sorry I worded the question very poorly so this isn't what I meant. I updated the question for more clarity.
Answer by JJJaden · Sep 04, 2020 at 05:25 AM
Also, If you imported any models, I have found that some models make my game really slow, glitchy and jerky. IDK if this helps but....