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?