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;
}
}