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

371
Visualizações
Jump function Unity

I updated my code, my current problem is just that the movement of the player doesn't work anymore and I would like to know what I have to do so that I can adjust the height of the jump and that when this height is reached, gravity comes through again Code:

//Private Variables
private CharacterController _chaCont;
private float currentGravity;
private Vector3 finalMovement; 
//Globally delared
                               
//Public Variables
public float speed;
public float gravity;
public float jumpSpeed;

// Start is called before the first frame update
void Start()
{
    _chaCont = gameObject.GetComponent<CharacterController>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
       finalMovement = jump() + ApplyGravity();
    }
    _chaCont.Move(finalMovement * Time.deltaTime);
}

Vector3 ApplyGravity()
{
    Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
    currentGravity += gravity * Time.deltaTime;

    if (_chaCont.isGrounded)
    {
        if (currentGravity > 1f)
            currentGravity = 1f;
    }

    return gravityMovement;
}

Vector3 Movement()
{
    Vector3 moveVector = Vector3.zero;

    moveVector += transform.forward * Input.GetAxis("Vertical");
    moveVector += transform.right * Input.GetAxis("Horizontal");
    moveVector *= speed;

    return moveVector;
}

Vector3 jump()
{
    Vector3 jumpVector = Vector3.zero;

    jumpVector += transform.up * Input.GetAxis("Jump");
    jumpVector *= jumpSpeed;

    return jumpVector;
}

}

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Good Night. Have you tried the input system C# provides should be like below code.

    //Private Variables
private CharacterController _chaCont;
private float currentGravity;
private Vector3 finalMovement; //Globally delared
//Public Variables
public float speed;
public float gravity;

// Start is called before the first frame update
void Start()
{
    _chaCont = gameObject.GetComponent<CharacterController>();
}

// Update is called once per frame
void Update()
{
    Vector3 initialMovement = Movement();
    if(Input.GetKeyDown(KeyCode.Space)){
       finalMovement = initialMovement + ApplyGravity();
     }
     else{
        finalMovement = initialMovement;
     }
    _chaCont.Move(finalMovement * Time.deltaTime);
}

Vector3 ApplyGravity()
{
    Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
    currentGravity += gravity * Time.deltaTime;

    if (_chaCont.isGrounded)
    {
        if (currentGravity > 1f)
            currentGravity = 1f;
    }

    return gravityMovement;
}

Vector3 Movement()
{
    Vector3 moveVector = Vector3.zero;

    moveVector += transform.forward * Input.GetAxis("Vertical");
    moveVector += transform.right * Input.GetAxis("Horizontal");
    moveVector *= speed;

    return moveVector;
}    
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