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

Help with moving player using rb.velocity

Discussion in 'Scripting' started by deen0_, Dec 28, 2021.

  1. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Hello everyone, I am having a bit of an issue trying to swap from using transform.Translate to rigidbody.velocity for player movement.

    It seems that my player moves okayish, a little juttery at times, but it also no longer seems to hit the ground?

    I have a isGrounded bool, set off a few meters above the correct ground.

    Note that groundCheckObject is an empty gameobject placed at the players.
    Code (CSharp):
    1.  
    2. private void Move()
    3.     {
    4.         // Grounded
    5.         isGrounded = Physics.CheckSphere(groundCheckObject.position, groundCheckDistance, groundMask);
    6.  
    7.         if (isGrounded && velocity.y < 0)
    8.         {
    9.             velocity.y = -2f;
    10.             // Sticks us to the ground
    11.         }
    12.  
    13.  
    14.  
    15.         Vector3 inputDir = transform.right * moveDirection.x + transform.forward * moveDirection.y;
    16.         inputDir = inputDir.normalized;
    17.      
    18.  
    19.         // Not moving if we not grounded
    20.         if (isGrounded)
    21.         {
    22.             // Sprint
    23.             if (canSprint && wantToSprint)
    24.             {
    25.                 speedMultiplier = sprintMultiplier;
    26.                 canDodge = false;
    27.             }
    28.             else
    29.             {
    30.                 speedMultiplier = initSpeedMult;
    31.                 canDodge = true;
    32.             }
    33.  
    34.             // Dodge
    35.             if (canDodge && wantToDodge && moveDirection != Vector2.zero)
    36.             {
    37.                 speedMultiplier = dodgeMultiplier;
    38.                 canSprint = false;
    39.                 gameObject.GetComponent<PlayerAttack>().canAttack = false;
    40.  
    41.                 StartCoroutine(Dodge());
    42.             }
    43.             else
    44.             {
    45.                 speedMultiplier = initSpeedMult;
    46.                 canSprint = true;
    47.                 gameObject.GetComponent<PlayerAttack>().canAttack = true;
    48.             }
    49.  
    50.             moveAmount = inputDir * moveSpeed * speedMultiplier;
    51.         }
    52.  
    53.         moveAmount.y += gravity * Time.fixedDeltaTime;// *Time.fixedDeltaTime
    54.         Debug.Log(moveAmount);
    55.      
    56.         gameObject.GetComponent<Rigidbody>().velocity = moveAmount;
    57.  
    58.         //velocity.y += gravity * Time.deltaTime;
    59.     }
    60.  
    I also tried checking this using collisions:
    Code (CSharp):
    1.  
    2. public void OnCollisionStay(Collision collision)
    3.     {
    4.  
    5.         Debug.Log(collision.gameObject.tag);
    6.     }
    This outputs that the collider is hitting the ground, although it shouldn't be.

    For reference I attached a photo of the capsule collider seemingly floating.

    I was wondering if you guys could help me out with this? Also wondering if there are any immediate improvements you guys can see within my velocity code, since this is my first time putting together the information.

    Thank you!
     

    Attached Files:

    Last edited: Dec 28, 2021
  2. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Here is the players rigidbody component and capsule collider if that is of any use
     

    Attached Files:

  3. deen0_

    deen0_

    Joined:
    Feb 10, 2021
    Posts:
    33
    Figured it out, the mesh Collider on my ground plane was on convex and was messing with the collision height.

    Does anyone know what convex is useful for?
     
    Kurt-Dekker likes this.
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    If you want to attach a Rigidbody and have physics apply to something, the collider has to be a primitive (box, capsule, sphere) or Convex. So for example, if you want a chair to get knocked over, you could use a Box collider for best performance, but it would look better if the shape was more accurate.. which is why you might use Convex. However, do note that the more complex the mesh the worse it will be for performance, and it can't be more than [I think] like 255 vertices, but if you make it convex anyway it will skip any more than that when processing the mesh, but it won't crash (at least in my experience).
     
    Kurt-Dekker likes this.