I have created my first code, having a character interact around the environment. I coded my character to that when I press the space bar, he will jump. But it looks like when I hold the space bar for more than 1 second, he keeps going up and up or it seems like he is double jumping as you can see here: ProblemJump.mp4 - Google Drive
This is basically the code I have for my character:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Animator Anim;
[SerializeField] float WalkSpeed = 0.001f;
[SerializeField] float RotSpeed = .001f;
[SerializeField] float RunSpeed = 0.001f;
private AnimatorStateInfo PlayerLayer;
// Start is called before the first frame update
void Start()
{
Anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
PlayerLayer = Anim.GetCurrentAnimatorStateInfo(0);
if (PlayerLayer.IsTag("MotionStop"))
{
//Character movement
if (Input.GetAxis("Vertical") > 0)
{
transform.Translate(0, 0, WalkSpeed);
Anim.SetBool("WalkForward", true);
}
if (Input.GetAxis("Vertical") < 0)
{
transform.Translate(0, 0, -WalkSpeed);
Anim.SetBool("WalkBackward", true);
}
if (Input.GetAxis("Vertical") == 0)
{
Anim.SetBool("WalkForward", false);
Anim.SetBool("WalkBackward", false);
}
if (Input.GetAxis("Horizontal") > 0)
{
transform.Rotate(0, RotSpeed * Time.deltaTime, 0);
Anim.SetBool("WalkForward", true);
}
if (Input.GetAxis("Horizontal") < 0)
{
transform.Rotate(0, -RotSpeed * Time.deltaTime, 0);
Anim.SetBool("WalkForward", true);
}
if (Input.GetButton("Fire1"))
{
Anim.SetBool("RunForward", true);
transform.Translate(0, 0, RunSpeed);
}
else
{
Anim.SetBool("RunForward", false);
}
}
if ((Input.GetButton("Jump")))
{
Anim.SetTrigger("Jump");
GetComponent<Rigidbody>().velocity = new Vector3(0.4f, 8, 0.4f);
}
if (GetComponent<Rigidbody>().velocity.y == 0)
{
Anim.SetBool("Jump", false);
}
}
}
Did anyone of you guys experience this? Any help will be appreciated.