- Home /
Stop function in another function,Player running in the air
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;
}
}
}
Answer by tormentoarmagedoom · Mar 04, 2020 at 08:59 AM
Hello there!
Its good you strat programming!. As you are doing very basic things, you have several tutorials on youtube. I recoomend to spend time watching tutorials about movement. Dont try to go fast! You still need to learn a looot of things.
But you are in the correct way!
(PS: For your problem, you need to add some "if conditions" to be sure the character is on the ground before jump or to control its velocity)
Good luck!
Your answer
Follow this Question
Related Questions
Glitches at running time. 0 Answers
Mathf.SmoothStep not quite working. 1 Answer
Your program is not working for whatever reason. 1 Answer
Fixed game resolution 1 Answer
terrain hole shader needs fixing 1 Answer