Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Jump issue?

Discussion in 'Scripting' started by AerionXI, Dec 24, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    I am trying to convert my Character Controller jump over to AddForce ( ) jump. How would I do that given the following code?

    Code (CSharp):
    1. controller.Move(Vector3.up * verticalVel * Time.deltaTime);
    Code (CSharp):
    1. void CheckGrounded() { isGrounded = tryingJump ? false : (Physics.Raycast(transform.position + (transform.up * .05f), Vector3.down, .2f, groundLayerMask)); }
    2. bool CheckUpperContact() { return (Physics.Raycast(transform.position + (transform.up * .5f), Vector3.up, 1.5f, groundLayerMask)); }
    3. bool CheckForwardContact() { return (Physics.Raycast(transform.position + (transform.up * .7f), transform.forward, .5f, groundLayerMask)); }
    4.  
    5. void Jump() {
    6.         bool wasGrounded;
    7.         wasGrounded = isGrounded;
    8.  
    9.         if (!canMove || speedBreak)
    10.             return;
    11.  
    12.         if (!isGrounded)
    13.         {
    14.             StartCoroutine(JumpCoroutine());
    15.             return;
    16.         }
    17.  
    18.         if (!isBoosting)
    19.             speedBooster.StopAll(false);
    20.  
    21.         isGrounded = false;
    22.         verticalVel = Mathf.Sqrt(jumpHeight * -2f * gravity);
    23.         anim.SetTrigger("Jump");
    24.  
    25.         StartCoroutine(JumpCoroutine());
    26.  
    27.         IEnumerator JumpCoroutine()
    28.         {
    29.             tryingJump = true;
    30.  
    31.             if (CheckForwardContact() && characterVelocity != 0 && !wasGrounded)
    32.             {
    33.                 wallJumped = true;
    34.                 direction *= -1;
    35.                 characterVelocity *= -1;
    36.                 verticalVel = Mathf.Sqrt((jumpHeight / 2) * -2f * gravity);
    37.             }
    38.  
    39.             yield return new WaitForSeconds(.05f);
    40.             tryingJump = false;
    41.             yield return new WaitForSeconds(1);
    42.             wallJumped = false;
    43.         }
    44. }
    45.  
    46. void Update()
    47. {
    48.         desiredJump |= input.actions["Jump"].WasPressedThisFrame();
    49.  
    50.         //Movement
    51.         CheckGrounded();
    52.         CheckDirection();
    53.         Move();
    54.         if (desiredJump)
    55.             desiredJump = false;
    56.             if (!isSliding)
    57.                 Jump();
    58.         }
    59. }
    Thanks in advance for the help and have a VERY Merry Christmas!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Steps to success:

    - understand that CC is NOT compatible with RB. You may have one or the other. Otherwise they fight over the same Transform.

    - choose CC or RB

    - implement.

    If you want a nice fully-functional super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    That doesn't answer my question at all. That is avoiding the question altogether. I'm NOT getting into a fight with you on Christmas Eve for crying out loud. I SPECIFICALLY asked, "using the following code, how do I change this 1 line ( controller.Move ) to use rb.AddForce" instead? Please stop giving me crap every time I post.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    You're establishing a long track record of hundreds of illogical posts, several of them verging on hysterical.

    I hope that you can set aside your emotions and deal with the underlying software engineering problem.

    It is clear you have not apprehended the first bullet point above.

    Perhaps someone else can help you understand my first point above.

    Steps to success:

    - remove the CharacterController

    - add a Rigidbody

    - reconnect all the movements you already have to instead manipulate the RB

    - choose a force or impulse value to use for your jump and connect to the jump.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    Merry Christmas!
     
  5. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164