As the title says, my movement is incredibly slow for what it should be and stuttering inconsistently.
I want to make a 3D platformer with tight controls and figure the best way to do that is by setting the velocity directly to the rigidbody.
I’m only using one script on a capsule for testing purposes. Here’s the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[Header("References")]
[SerializeField] Rigidbody rb;
[SerializeField] PlayerInput input;
#region Input Variables
bool jumpPressed = false;
bool boostPressed = false;
bool movePressed = false;
Vector2 moveInput;
#endregion
#region Physics Variables
float groundedGravity = -0.5f;
[SerializeField, Range(1f, 5f)] float standardSpeed = 2f, boostSpeed = 2f;
[SerializeField, Range(10f, 20f)] float maxSpeed = 10f;
Vector3 appliedMovement;
#endregion
#region General Functions
void Awake()
{
rb = GetComponent<Rigidbody>();
input = new PlayerInput();
rb.useGravity = false;
appliedMovement = new Vector3();
input.player.move.started += OnMove;
input.player.move.performed += OnMove;
input.player.move.canceled += OnMove;
input.player.jump.started += OnJump;
input.player.jump.canceled += OnJump;
input.player.boost.started += OnBoost;
input.player.boost.canceled += OnBoost;
}
void FixedUpdate()
{
CalculateGroundMovementSpeed();
rb.velocity = appliedMovement;
}
#endregion
#region Input Functions
void OnEnable()
{
input.Enable();
}
void OnDisable()
{
input.Disable();
}
void OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
movePressed = moveInput.x != 0 || moveInput.y != 0;
appliedMovement.x = moveInput.x;
appliedMovement.z = moveInput.y;
}
void OnJump(InputAction.CallbackContext context)
{
jumpPressed = context.ReadValueAsButton();
}
void OnBoost(InputAction.CallbackContext context)
{
boostPressed = context.ReadValueAsButton();
}
#endregion
void CalculateGroundMovementSpeed()
{
if (boostPressed){
appliedMovement.x *= standardSpeed * boostSpeed * Time.deltaTime;
appliedMovement.z *= standardSpeed * boostSpeed * Time.deltaTime;
}
else{
appliedMovement.x *= standardSpeed * Time.deltaTime;
appliedMovement.z *= standardSpeed * Time.deltaTime;
}
}
#region Collision Functions
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "ground")
{
appliedMovement.y = groundedGravity;
}
}
#endregion
}
Here are the details in the inspector.
If you need any more details, please let me know.
Thank you for your time.