- Home /
Little Question regarding rigidbody gravity and addforce.
Hi,
I'm having some problems with AddForce not working after I set the use gravity of my rigidbody to false.
So my question is: Does Addforce work when gravity is turned off?
Thanks in advance!
~Nick
EDIT:
Hmm I think I already figured out what went wrong, still seems weird tho, whenever I change the dodge keys in my script it stops working. the default A and D keys work fine. it seems like it has something to do with my character already moving when the "dodge" is initiated, as A and D are also used to move left and right normally. When changed to Q and E it stops working. Here's a bit of code:
#pragma strict
var dodgeKeyRight: KeyCode = KeyCode.D;
var dodgeKeyLeft: KeyCode = KeyCode.A;
var lastTime: float = -1.0f;
var player: GameObject;
var speed: float = 100;
var curHor: float;
var curVert: float;
var curJump: float;
var hitGround: boolean = false;
var baseSpeed: Vector3;
var baseSideways: Vector3;
var maxSpeed: float = 2;
var shootScript: Shooter;
function Awake()
{
player = GameObject.FindWithTag("Player");
shootScript = player.GetComponent(Shooter);
}
function FixedUpdate()
{
CheckGround();
if(CheckSpeed())
{
Movement();
Dodge();
}
if(shootScript.pullIt == true)
{
player.rigidbody.useGravity = false;
}
else if(shootScript.pullIt == false)
{
player.rigidbody.useGravity = true;
}
}
function Dodge()
{
if (Input.GetKeyDown(dodgeKeyRight))
{
if (Time.time - lastTime < 0.3f)
{
lastTime = -1.0;
player.rigidbody.AddForce(transform.right*speed*5);
player.rigidbody.AddForce(transform.up*speed*3);
}
else
{
lastTime = Time.time;
}
}
if (Input.GetKeyDown(dodgeKeyLeft))
{
if (Time.time - lastTime < 0.3f)
{
lastTime = -1.0;
player.rigidbody.AddForce(-transform.right*speed*5);
player.rigidbody.AddForce(transform.up*speed*3);
}
else
{
lastTime = Time.time;
}
}
}
function Movement()
{
curHor = Input.GetAxis("Horizontal");
curVert = Input.GetAxis("Vertical");
curJump = Input.GetAxis("Jump");
if(hitGround)
{
baseSpeed = transform.forward*curVert*speed/2;
baseSideways = transform.right*curHor*speed/2;
player.rigidbody.AddForce(baseSideways);
player.rigidbody.AddForce(baseSpeed);
player.rigidbody.AddForce(transform.up*curJump*speed*5);
}
if(!Input.GetAxis("Vertical") && !Input.GetAxis("Horizontal") && hitGround)
{
player.rigidbody.drag = 100;
}
else
{
player.rigidbody.drag = 0;
}
if(Input.GetAxis("Jump"))
{
player.rigidbody.drag = 0;
}
}
function CheckGround()
{
var hit: RaycastHit;
if (Physics.SphereCast (transform.position, 0.5, -transform.up, hit, .5))
{
hitGround = true;
}
else if (!Physics.SphereCast (transform.position, 0.5, -transform.up, hit, .5))
{
hitGround = false;
}
}
function CheckSpeed():boolean
{
if(player.rigidbody.velocity.z < maxSpeed &&
player.rigidbody.velocity.z > -maxSpeed &&
player.rigidbody.velocity.x < maxSpeed &&
player.rigidbody.velocity.x > -maxSpeed)
{
return true;
}
}
Answer by whydoidoit · Jun 01, 2012 at 08:13 AM
If you haven't changed the keys in the input manager then rigid body.drag will be set to 100 if the dodge keys don't match the setting for the horizontal axis. Per your Movement function.
Oh! Now I get it^^; It seemed so illogical, but it makes sense now, thanks! ^^
Answer by Wolfram · May 31, 2012 at 01:44 PM
Yes it does, your problem must lie somewhere else. Maybe you could post the part of your script dealilng with forces, and elaborate on "some problems with AddForce"?
Note that with "isKinematic" enabled, forces/physics will be ignored.
Your answer
Follow this Question
Related Questions
push crate - works in editor, not on device 2 Answers
Rotation problem with spawning bullet 2 Answers
How can I turn on/off Rigidbody.useGravity? 0 Answers
Top Down car/vehicle movement 1 Answer
how get bow to add more force over time 0 Answers