I started coding in unity a couple of days ago. I’m still trying to grasp and learn the fundamentals and while creating a character controller I found out that with what I made, the player continues to run mid-air if shift is held down and the player can jump even while falling off a ledge. I would like to prevent the player from running mid-air and prevent them from being able to jump while falling down.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCharacterControl : MonoBehaviour
{
public float Speed;
private float canJump = 0f;
void Update ()
{
PlayerMovement();
PlayerSpeed();
}
void PlayerSpeed()
{
if(Input.GetKey(KeyCode.LeftShift))
{
Speed = 22.0f;
}
else
{
Speed = 12.0f;
}
}
void PlayerMovement()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 playerMovement = new Vector3(hor, 0f, ver) * Speed * Time.deltaTime;
transform.Translate(playerMovement, Space.Self);
if (Input.GetKeyDown ("space") && Time.time > canJump)
{
Vector3 up = transform.TransformDirection (Vector3.up);
GetComponent<Rigidbody>().AddForce (up * 7, ForceMode.Impulse);
canJump = Time.time + 1.24f;
}
}
}