Question by
adibak · Jun 28, 2021 at 11:28 PM ·
movementfpskeyboard input
How to smoothen FPS movement (keyboard)
Hi,
I'm making a 3d FPS game in Unity and I have this code so far for the movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementController : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private float jumpForce;
[SerializeField] private float rayCastDist;
private Rigidbody rigidbody;
private void Start(){
rigidbody = GetComponent<Rigidbody>();
}
private void Update(){
Jump();
}
private void FixedUpdate(){
Move();
}
private void Move(){
float hAxis = Input.GetAxis("Horizontal");
float yAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, yAxis) * speed;
Vector3 newPos = rigidbody.position + rigidbody.transform.TransformDirection(movement);
rigidbody.MovePosition(newPos);
}
private void Jump(){
if (Input.GetKeyDown(KeyCode.Space)){
if (IsGrounded()){
rigidbody.AddForce(0, jumpForce, 0, ForceMode.Impulse);
}
}
}
private bool IsGrounded(){
return (Physics.Raycast(transform.position, Vector3.down, rayCastDist));
}
}
When I run this, my player (a capsule) moves with my key inputs. But, it is a bit choppy. For example, when I press an arrow key, the player moves abruptly with a jolt after like 3/4 of a second. Can someone please help me with how I can smoothen this movement? thanks.
Comment
Your answer
Follow this Question
Related Questions
Why does the player moving acceleration increase with more fps? 0 Answers
First Person Slide in direction of rigidbody movement and not look direction 1 Answer
How do I add Jumping in this Script 0 Answers
How to move character FPS mode 0 Answers
How to make changing direction in my FPS game less jerky? 3 Answers