Answer the question
In order to leave comments, you need to log in
Jumping in Unity. How to make a character jump once?
When you press the spacebar, the character jumps, but if you press the spacebar again during the flight, he will jump higher and so on until you press the spacebar. How to make a character jump once?
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour
{
[SerializeField]
private int lives = 5;
[SerializeField]
private float speed = 3.0F;
[SerializeField]
private float jumpForce = 15.0F;
private bool isGrounded = false;
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
if (Input.GetButton("Horizontal")) Run(); }
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x < 0.0F;
}
private void Jump()
{
rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
isGrounded = colliders.Length > 1;
}
}
Answer the question
In order to leave comments, you need to log in
https://habrahabr.ru/post/212309/
In this article you will find the answer to your question)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question