So my problem is that when my character is gliding after pressing space, I want to be able to press space again to stop gliding and I don’t know how.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Controller2D))]
public class Player : MonoBehaviour {
public float jumpHeight = 4;
public float timeToJumpApex = .4f;
float accelerationTimeAirborne = .2f;
float accelerationTimeGrounded = .1f;
float moveSpeed = 8;
public bool IsGliding = false;
float Fallspeed = 4;
float gravity;
float jumpVelocity;
Vector3 velocity;
float velocityXSmoothing;
Controller2D controller;
void Start() {
controller = GetComponent<Controller2D> ();
gravity = -(2 * jumpHeight) / Mathf.Pow (timeToJumpApex, 2);
jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
print ("Gravity: " + gravity + " Jump Velocity: " + jumpVelocity);
}
void Update() {
if(IsGliding && velocity.y < 0f && Mathf.Abs(velocity.y) > Fallspeed)
{
velocity = new Vector3(velocity.x, Mathf.Sign(velocity.y) * Fallspeed);
}
if (controller.collisions.above || controller.collisions.below) {
velocity.y = 0;
}
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
if(Input.GetButtonDown("Jump") && !controller.collisions.below)
{
GlideStart();
}
if (Input.GetButtonDown("Jump") && controller.collisions.below) {
velocity.y = jumpVelocity;
GlideStop();
}
float targetVelocityX = input.x * moveSpeed;
velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
velocity.y += gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
public void GlideStart()
{
IsGliding = true;
}
public void GlideStop()
{
IsGliding = false;
}
}