- Home /
AddForce with direction tuning
Hello everybody. I´m using this script for moving my Rigidbody Capsule Character.
void FixedUpdate () {
//Only move if on a solid ground
if (onGround && collisionAngle > slopeAngle) {
//Get desired velocity
velDesired = new Vector3( Input.GetAxis("Horizontal"), 0.0F, Input.GetAxis("Vertical") );
velDesired = transform.TransformDirection(velDesired);
speed = walkSpeed * ( bool2Int( Input.GetKey(KeyCode.LeftShift) )+1 );
velDesired *= speed;
//Get current velocity
velCurrent = rigidbody.velocity;
//Get the difference in between
velDifference = velDesired - velCurrent;
velDifference.y = 0.0F;
//Apply the difference to body
rigidbody.AddForce(velDifference, ForceMode.VelocityChange);
//Get if want to jump
if ( Input.GetButton("Jump") ) {
//float velJump = Mathf.Sqrt(2.0F * jumpHeight * gravity);
velCurrent = rigidbody.velocity;
rigidbody.velocity = new Vector3(velCurrent.x, velJump, velCurrent.z);
//onGround = false;
}
}
else {
rigidbody.AddForce( new Vector3(0.0F, -gravity * rigidbody.mass, 0.0F) );
}
}
Everything is fine, I´d like to tune it a bit. I remove "onGround" check in the first if, because I want to move it in the air as well. It works, but if I addForce and a vector which is a direction to my Rigidbody by another collider when I´m in air, I can´t move it anymore. Does anyone know where the problem is, please? :)
Thanks JarPet
This isn't clear: but if I addForce and a vector which is a direction to my Rigidbody by another collider when I´m in air. The character is flying, ok, but the other factors that cause the issue are very obscure. You're trying to change direction, collides with something and the character gets stuck, is it? And it doesn't happen when you're on the ground?
Hi, I think I'm having a similar problem, I'm trying to shoot a bullet and it's standing on the air without moving when I use bullet.rigidbody.addForce(direction);
but that works if the bullet is grounded (the ball moves on rolling on the ground).
What I want is to move the bullet on the air (freezing the rigidbody in y-position) but it doesn't work then.
Can anyone help?
To follow up: the script above is working? The problem is when you make some changes that we don't know about?
One cheesy sidestep can be to (temporarily) turn off useGravity.
Just made it work with bullet.rigidbody.AddForce(bullet.transform.forward*250), freezing y-position.
Don't know exactly why this got so easily solved oO