- Home /
How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces?
I'm trying to make an FPS controller that reacts to forces and has snappy and sharp movements. I first tried with transform.translate but that dosen't react to forces and it glitched when colliding with walls. Then I tried using rigidbody.MovePosition wich worked really well untill I tried to add forces to it and realized that rigidbody.MovePosition doesn't affect velocity, resulting in the velocity just getting saved when and not altered when I move. After that I tried to directly change the velocity but that just cancels out all the forces applied. rigidbody.AddForce did react to forces correctly but it felt like it was too slow to accelerate when pressing a move key. I am trying to get instant acceleration that gets affected by forces but I don't know how to do it. Can someone please help?
Answer by Kciwsolb · Jun 01, 2018 at 03:29 PM
One option (I don't like this as much as the next option) would be to add an Impulse to the rigidbody the first time the key is pressed down (GetButtonDown), then add a continuous force after that (GetButton) to keep it at some speed.
Another option (better but more complicated) is to use AddForce with ForceMode.VelocityChange, but account for your current velocity when calculating your velocity change. Look at how this example calculates its movement.
I implemented something similar to that example in a project. It lets me use Physics to move the player and still be affected by forces. Here is a gif of it being used. The player is walking in a wind zone. The wind zone is just a script that applies a force to every Rigidbody in its trigger each FixedUpdate. As you can see, he is still affected by the wind even though I am changing the velocity.
Answer by badtanman2 · Apr 23, 2019 at 07:28 AM
ForceMode.VelocityChange works out okay but personally I found it to be lacking the smooth movement I desired. ForceMode.Acceleration worked perfectly for me.
Making the force variable greater than maxSpeed will ensure your object will accelerate quickly (I used force = 200 and maxSpeed = 16 which worked out nicely) and play around with your rigidbody's drag value to suit the slide/deceleration to your liking.
using UnityEngine;
public class FPS_Move : MonoBehaviour
{
public float _force, _maxSpeed;
[SerializeField]
private float _currentSpeed, z, x;
private Vector3 _direction;
private Rigidbody _rBody;
void Start()
{
_direction = Vector3.zero;
_rBody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//For Debugging
_currentSpeed = _rBody.velocity.magnitude;
if (_currentSpeed < 0)
{
_currentSpeed = 0;
}
x = Input.GetAxisRaw("Horizontal");
z = Input.GetAxisRaw("Vertical");
if (x != 0 || z != 0)
{
_direction = transform.right * x + transform.forward * z;
_direction.Normalize();
_rBody.AddForce(_direction * _force, ForceMode.Acceleration);
if (_rBody.velocity.magnitude > _maxSpeed)
{
_rBody.velocity = _rBody.velocity.normalized * _maxSpeed;
}
}
}
}