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

334
Visualizações
Jump in using Rigidbody or Character controller

i have been recreating my jump code, i have it all done but i can't add force or anything else. Here's my code

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

public class pBeh : MonoBehaviour
{
CharacterController characterController;
public float MovementSpeed = 1;
public float Gravity = 9.8f;
private float velocity = 0;
private Camera mainCam;
public Rigidbody rb;
public float jumpSpeed = 5.2f;
private Vector3 movingDirection = Vector3.zero;

public CharacterController controller;
public float speed;
float turnSmoothVelocity;
public float turnSmoothTime;
public bool canJump = false;


private void Start()
{
    characterController = GetComponent<CharacterController>();
    mainCam = Camera.main;
}

void Update()
{
    // player movement - forward, backward, left, right
    float horizontal = Input.GetAxis("Horizontal") * 10;
    float vertical = Input.GetAxis("Vertical") * 10;
    Vector3 camRightFlat = new Vector3(mainCam.transform.right.x, 0,
            mainCam.transform.right.z).normalized;
    Vector3 camForwardFlat = new Vector3(mainCam.transform.forward.x, 0,
            mainCam.transform.forward.z).normalized;
    characterController.Move((camRightFlat * horizontal + camForwardFlat * vertical)
            * Time.deltaTime);


    // Gravity
    if (characterController.isGrounded)
    {
        velocity = 0;
    }
    else
    {
        velocity -= Gravity * Time.deltaTime;
        characterController.Move(new Vector3(0, velocity, 0));
    }
    if (canJump == true && Input.GetKeyDown(KeyCode.Space))
    {

        Debug.Log("Jumped");/*
        transform.Translate(Vector3.up * 5.0f * Time.deltaTime);
        rb.AddRelativeForce(Vector3.up * 8.0f);
        rb.AddForce(Vector3.up * 8.0f)
        i have tried a lot more, but it just doesn't work.
        The sphere (Player) does nothing or shakes.
        */


    }
    
    




        
}
private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.tag == "enemy")
    {
        SceneManager.LoadScene("SampleScene");
    }
}
void FixedUpdate()
{
    if ((controller.collisionFlags & CollisionFlags.Below) != 0)
    {
        //Debug.Log("ground");
        canJump = true;
        
    }
    else
    {
        canJump = false;
    }
    
    

}

As i said it works just fine i just can't find reason why i cannot jump. I have all character controller right and rigidbody too. If you could help i would be happy. Btw i am beginner so i copied movement to be same as camera. Thanks!

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

0

From what I know, rigidbody and character controller do not work together (on the same object) so you have to choose one or the other, if you are using rigidbody then a ground check or similiar stuff would not be necessary since rigidbody is using unity physicis system so a lot of the stuff would be already done for you to simulate physics but you should still read thru the document

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