- Home /
Question by
MortalWombat922 · Mar 24, 2015 at 06:20 AM ·
rigidbodycontrollervelocitymomentum
Is it possible to increase stopping distance with forcemode.velocitychange?
So, here's the code I'm working with:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]
public class FirstPersonCharacterController : MonoBehaviour {
public Vector3 targetVelocity = Vector3.zero;
public float speed = 10.0f; //Speed in meters per second.
public float gravity = 9.81f; //Acceleration due to gravity in merers per second per second.
public float maxVelocityChange = 5.0f; //Acceleration in meters per second per second.
public float StoppingDeceleration = -1.0f; //Deceleration when stopping.
public bool canJump = true;
public float jumpHeight = 2.0f;
private bool grounded = false;
public float mousesensitivity = 5.0f;
public float xaxis = 0.0f;
public float zaxis = 0.0f;
public float zrange = 90.0f;
void Awake () {
//GetComponent<Rigidbody> () = true;
GetComponent<Rigidbody> ().useGravity = false;
}
void FixedUpdate () {
if (grounded) {
// Set target velocity to inputs.
targetVelocity.x = Input.GetAxis("Horizontal");
targetVelocity.y = 0.0f;
targetVelocity.z = Input.GetAxis("Vertical");
// Make acceleration relative to character rotation rather than worldspace.
targetVelocity = transform.TransformDirection(targetVelocity);
Debug.Log (targetVelocity);
targetVelocity *= speed;
// Calculate the difference between the velocity we want, and the velocity we have.
Vector3 velocity = GetComponent<Rigidbody> ().velocity;
Vector3 velocityChange = (targetVelocity - velocity);
//Limit maximum velocity change to maximum acceleration.
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
//Add force while walking/running.
GetComponent<Rigidbody> ().AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump && Input.GetButton("Jump")) {
GetComponent<Rigidbody> ().velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
GetComponent<Rigidbody> ().AddForce(new Vector3 (0, -gravity * GetComponent<Rigidbody> ().mass, 0));
grounded = false;
//Rotation
xaxis = Input.GetAxis ("Mouse X") * mousesensitivity;
transform.Rotate (0, xaxis, 0);
zaxis -= Input.GetAxis ("Mouse Y") * mousesensitivity;
zaxis = Mathf.Clamp (zaxis, -zrange, zrange);
Camera.main.transform.localRotation = Quaternion.Euler (zaxis, 0, 0);
}
void OnCollisionStay () {
grounded = true;
}
float CalculateJumpVerticalSpeed () {
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
}
It works alright as a traditional FPS character controller. What I'd like to change is no matter what I set the speed to, it takes the same amount of time for the character to stop. Is it possible to add in variable stopping distance depending on how fast they are moving? For example, when humans sprint, it can sometimes take them a good few meters for them to stop completely if they don't want to use a lot of energy stopping quickly.
Comment