Character keeps jumping without pressing the spacebar
i’m using unity c# please help!
my code:
using UnityEngine;
using System.Collections;
public class smileycontroller : MonoBehaviour
{
public float maxspeed = 10f;
bool facingRight = true;
Animator anim;
SpriteRenderer sr;
public bool Left_right { get; private set; }
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
float jumpTime, jumpDelay = .5f;
bool jumped;
void Start()
{
anim = GetComponent<Animator>();
sr = this.GetComponent<SpriteRenderer>();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
float move = Input.GetAxis("Horizontal");
if (Input.GetKeyDown(KeyCode.Space) && grounded)
GetComponent<Rigidbody2D>().AddForce(transform.up * 700f);
anim.SetTrigger("Jump");
jumpTime = jumpDelay;
{
jumpTime -= Time.deltaTime;
if (jumpTime <= 0 && grounded)
anim.SetTrigger("Land");
}
anim.SetFloat("speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxspeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip();
else if (move < 0 && facingRight)
Flip();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
Left_right = !Left_right;
sr.flipX = !sr.flipX;
}
}