Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
using Vector3.up make th player do a more than 1 jump

Hi I'm new to c# and i just made a code that allow the player to jump and it worked fine but the problem is if i clicked the space button twice it will do a douple jump and if i keep doing it will fly.... I just want a 1 jump and if the player hit the ground can jump again no double jump ... What i have tried is to change the number that Multiply with vector3.up to just jump and made a

public float jump = 5f;

also didnt work any way here is my source code hope you understand the problem and help me

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;
    public float speed1;
    public float jump = 5f;


    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        transform.Translate(Vector3.forward * speed1);

        if (Input.GetKeyDown(KeyCode.A))
        {
            transform.Translate(-speed * Time.deltaTime, 0, 0);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            transform.Translate(speed * Time.deltaTime, 0, 0);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            GetComponent<Rigidbody>().AddForce(Vector3.up * jump, ForceMode.VelocityChange);
        }
    }
}
 
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can create a flag wasGrounded which you will set false after jumping. The player can only jump, when wasGrounded == true. You also add a collider to your ground. Once the player touched the ground, you reset the wasGrounded flag to true.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;
    public float speed1;
    public float jump = 5f;

    private bool wasGrounded = true;


    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        transform.Translate(Vector3.forward * speed1);

        if (Input.GetKeyDown(KeyCode.A))
        {
            transform.Translate(-speed * Time.deltaTime, 0, 0);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            transform.Translate(speed * Time.deltaTime, 0, 0);
        }

        if (Input.GetKeyDown(KeyCode.Space) && wasGrounded)
        {
            wasGrounded = false;
            GetComponent<Rigidbody>().AddForce(Vector3.up * jump, ForceMode.VelocityChange);
        }
    }

    void OnCollisionEnter(Collision other)
    {
        // Change to string you compare it to to the tag of your ground gameobject.
        if (other.gameObject.tag == "Ground")
        {
            wasGrounded = true;
        }
    }

}

Make sure to have "IsTrigger" set false on the ground`s collider.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda