- Home /
How to AddForce OverTime?
Hi there, I wanted to add into a FPS the ability to use lift like found in Bungie's game Destiny, I was able to get a second jump to occur after leaving the ground but I can't get it to sustain the added force for a given time. Here's my code:
//Walking variables
var walkAcceleration : float = 5;
var walkDeacceleration : float = 5;
var walkAccelAirRatio : float = 0.3;
@HideInInspector
var walkDeaccelerationVolX : float;
@HideInInspector
var walkDeaccelerationVolZ : float;
var cameraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontalMovement : Vector2;
//Jumping variables
var jumpVelocity : float = 40;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
//Lift variables
var liftVelocity : float = 20;
var liftDuration : float = 3;
var liftCDTime : float = 5;
var usingLift : boolean = false;
function Start ()
{
}
function Update ()
{
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement.Normalize();
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if(grounded)
{
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolX, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolZ, walkDeacceleration);
liftDuration = 3;
usingLift = false;
}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if(grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio * Time.deltaTime);
if(Input.GetButtonDown("Jump") && grounded)
rigidbody.AddForce(0, jumpVelocity, 0);
if(Input.GetButtonDown("Jump") && !grounded)
{
usingLift = true;
if(liftDuration > 0)
{
rigidbody.AddForce(0, liftVelocity * liftDuration, 0);
}
}
if(usingLift)
liftDuration -= Time.deltaTime;
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
{
grounded = true;
}
}
}
function OnCollisionExit ()
{
grounded = false;
}
It counts down the liftDuration variable properly but aside from the initial force exerted when pressing the jump key it isn't sustained so I can't make it seem like you are hovering using a thruster or jump pack. Any ideas on how I can fix this?
Answer by dmg0600 · Sep 19, 2014 at 05:13 PM
The problem is you are using Input.GetButtonDown("Jump") for the lift effect instead of Input.GetButton("Jump").
Input.GetButtonDown only triggers at the exact time the key is pressed while Input.GetButton triggers every frame the key is maintined.
Change the line 60 from if(Input.GetButtonDown("Jump") && !grounded) to if(Input.GetButton("Jump") && !grounded)
Other than that you just have to adjust the lift velocity according to the mass of the rigidbody and the lift velocity that you want.
Wouldn't that make it so lift only occurs when holding the jump button? I want it to be trigger that occurs when you press the jump key for the allotted time.
Ok, I missunderstood. To do so you have to move the rigidbody.AddForce from the if.
if(Input.GetButtonDown("Jump") && !grounded)
{
usingLift = true;
}
if(usingLift && liftDuration > 0)
{
liftDuration -= Time.deltaTime;
rigidbody.AddForce(0, liftVelocity/* * liftDuration*/, 0);
}
That way when you press jump when in the air, the lift effect is activated by setting the variable usingLift to true.
While it is true and the liftDuration hasn't come to 0, it will reduce the liftDuration and apply the force.
Glad to hear that! Please mark it as answered. If you have further questions, don't hesitate in asking.
Your answer
Follow this Question
Related Questions
Adding force to rigidbody 1 Answer
About Forces and Time.deltaTime 3 Answers
How to set max speed for this car ? 1 Answer
How to make an object wait until it finishes moving? 0 Answers
Reading the forces applied on a freezed rigidbody(solved) 1 Answer