- Home /
Adding an upward force
I'm trying to add an upward force on my character to give a gliding feel when he is in midair, but it doesn't seem to work.
Here is the related code:
if (Input.GetButton ("Fire1")) {
Debug.Log("Glide");
rigidbody.AddForce (0,10,0);
There is a rigidbody attached to the object. The scene is just a simple cube as character and plane as floor so nothing else should be interfering with it.
I also tried adding a constant force component which didn't work either
If anyone could help would be greatly appreciated
Answer by Kleptomaniac · Mar 03, 2012 at 12:48 AM
Hey there, moving comment to answer so people can see what was wrong:
The problem was that while Fire1 was pressed, moveDirection.y is still being minuses. Therefore the glide and vertical position modifiers were cancelling each other out. Try this:
if (Input.GetButton ("Fire1")) {
Debug.Log("Glide");
rigidbody.AddForce (0,100,0);
} else {
moveDirection.y -= gravity * Time.deltaTime;
}
Thanks also to @Berenger for help!
Answer by Berenger · Mar 02, 2012 at 05:00 AM
If pushing doesn't work, push harder. That's my philosophy.
However, you could disable it's gravity as well.
i tried 10, 100 and 10,000 all with no result Also I don't want to disable or turn down gravity as it will have a strange effect on an upward jump
Answer by Berenger · Mar 02, 2012 at 07:37 AM
Are you manually modifying the velocity ? That would cancel the AddForce. Also, the debug line gets printed right ?
Yes the debug line gets printed. I am modifying the velocity im not sure how else to make a jump :P
my entire code is as follows, if you could point out my issue that would be great
var walkSpeed : float = 20.0; var gravity = 10.0; private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController; var jumpSpeed = 3; var player : GameObject;
function Start() { charController = GetComponent(CharacterController); animation.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop; }
function Update () { if(charController.isGrounded == true) {
if(Input.GetAxis("Vertical") > .1) {
}
else if(Input.GetAxis("Vertical") < -.1)
{
animation["walk"].speed = -1;
animation.CrossFade("walk");
walkSpeed = 20;
}
else {
animation.CrossFade("stop");
}
if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical")) // Create an animation cycle for when the character is turning on the spot
{
animation.CrossFade("walk");
}
transform.eulerAngles.y += Input.GetAxis("Horizontal");
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
charController.$$anonymous$$ove(moveDirection * (Time.deltaTime * walkSpeed));
if (Input.GetButton ("Fire1")) { //GLIDE FUNCTION HERE
Debug.Log("Glide");
rigidbody.AddForce (0,100,0);
}
}
you can create a new scene with a simple cube character and comment out the animate commands to test it
I believe your problem is that while Fire1 is pressed, moveDirection.y is still being $$anonymous$$used! Therefore your glide and vertical position modifier are cancelling each other out. Possibly try this:
if (Input.GetButton ("Fire1")) { //GLIDE FUNCTION HERE
Debug.Log("Glide");
rigidbody.AddForce (0,100,0);
} else {
moveDirection.y -= gravity * Time.deltaTime;
}
I think that should work ...
Isn't character controller supposed to apply gravity on it's own ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613064651im_/https://answers.unity.com/themes/thub/images/avi.jpg)