I’m trying to create movement system where you can switch from walking to flying. I’m doing it by changing players rigidbodys isKinematic off when not Grounded. While not Grounded pressing space down would add constant force towards rigidbody’s forward. The problem is, that when I have jumped, and Grounded has became false, the force I applied while space was down is still affecting me without signs of slowing. I tried to create trigger with boolean, that would allow force to be applied only when it’s true, and getting space up would turn it into false. However, this didn’t change anything.
I’m sorry i don’t have any code for it, as I have deleted and written it so many times, as its only few lines long. What I’m asking is how can I stop the force pushing player. Also, should I use rigidbodys AddForce or ConstantForce component for this?
When you jump, you apply force to the player, causing it to move. When you turn IsKinematic off, that makes the player immune to Forces, including gravity. That means your player will continue to move in the same direction without being able to stop.
If you don’t want the character to be affected by gravity, you can either turn UseGravity off, or apply negative gravity.
Remember, gravity uses ForceMode.Acceleration, and it must be applied every FixedUpdate.
AddForce will work for movement, and ConstantForce is just extra work for the same thing. The real problem with AddForce for movement is that the force keeps adding up every FixedUpdate. To get the amount of force you want, use rigidbody.AddForce(DesiredVelocity - rigidbody.velocity);
Past that, everything else depends on how you personally want your character to move. You could use Acceleration, which would make gravity work a little more smoothly, but friction will cause problems. VelocityChange would make your character move at exactly the speed you want, but I’m not sure how it affects gravity.
Rigidbody characters are tough, but worth it. I’ve been working on a controller for some time, which I’ll make available on the assets store when it’s finished.
Okay, after altering the code several times, I have got a simple piece of code that works, almost.
The script should work as a jetpack.
function Update () {
if ( Input.GetButton("Jump")) {
constantForce.force = 20* Vector3.up;
}
else {
constantForce.force = Vector3.zero;
}
}
It indeed pushes character up as it should, but when I add Character Controller to character, this script stops working. I’m using script from reference:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
I believe the built-in Character Controller is a Kinematic rigidbody (or the equivalent), and isn’t affected by forces.