Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug collision glitch

Discussion in 'Physics' started by McXbox, May 15, 2024.

  1. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    im having a glitch where when i touch something it pushs me, but only when i hit a certian side, heres the clip link

    after i jump and hit the block im not touching anything but my mouse to turn my camera
    if theres any way to fix this plese let me know
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    2,007
    Just looks like you're using no friction?
     
  3. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    @arkano22 ya,had i this problem and no friction fixed it but it cme back
     
  4. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    @arkano22 i changed my friction to 1000 it did nothing
     
  5. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    2,007
    How are you moving your character? Could you share the script you're using?
     
  6. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    @arkano22 ya heres my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 5.0f;
    9.     public float jumpForce = 5.0f;
    10.     public bool isOnGround = true;
    11.     private float horizontalInput;
    12.     private float forwardInput;
    13.     private Rigidbody playerRb;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         playerRb = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         // get player input
    25.         horizontalInput = Input.GetAxis("Horizontal");
    26.         forwardInput = Input.GetAxis("Vertical");
    27.  
    28.         //Move player forward
    29.         Vector3 movement = new Vector3(horizontalInput, 0.0f, forwardInput) * speed * Time.deltaTime;
    30.         playerRb.MovePosition(transform.position + transform.TransformDirection(movement));
    31.  
    32.         // let player jump
    33.         if (Input.GetKeyDown(KeyCode.Space) & isOnGround)
    34.         {
    35.             playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    36.             isOnGround = false;
    37.         }
    38.  
    39.     }
    40.     private void OnCollisionEnter(Collision collision)
    41.     {
    42.          
    43.  
    44.         if (collision.gameObject.CompareTag("Ground"))
    45.         {
    46.             isOnGround = true;
    47.         }
    48.     }
    49.    
    50. }
    51.  
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,644
    You should not be using MovePosition on a 3D Dynamic body. it's meant for Kinematic bodies. In 3D when you use it, it simply (instantly) teleports to that position.

    You should also only perform a MovePosition in FixedUpdate as that's when physics (by default) runs. Another minor note but when referring to the position of the body (or rotation) you should directly use the Rigidbody positon/rotation and not the Transform. The Transform is for visuals and other components, it won't be at the pose of the Rigidbody if you're using Interpolation or Extrapolation.

    I'd deal with these first before further investigation.
     
  8. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    @MelvMay I used a different movement tutorial and it fixed the physics problem but now this is happening


    heres my code now
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.    
    8.     public float walkSpeed = 6f;
    9.     public float runSpeed = 12f;
    10.     public float jumpPower = 7f;
    11.     public float gravity = 10f;
    12.     public float defaultHeight = 2f;
    13.    
    14.  
    15.     private Vector3 moveDirection = Vector3.zero;
    16.     private CharacterController characterController;
    17.  
    18.     private bool canMove = true;
    19.  
    20.     void Start()
    21.     {
    22.         characterController = GetComponent<CharacterController>();
    23.        
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    29.         Vector3 right = transform.TransformDirection(Vector3.right);
    30.  
    31.         bool isRunning = Input.GetKey(KeyCode.LeftShift);
    32.         float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
    33.         float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
    34.         float movementDirectionY = moveDirection.y;
    35.         moveDirection = (forward * curSpeedX) + (right * curSpeedY);
    36.  
    37.         if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    38.         {
    39.             moveDirection.y = jumpPower;
    40.         }
    41.         else
    42.         {
    43.             moveDirection.y = movementDirectionY;
    44.         }
    45.  
    46.         if (!characterController.isGrounded)
    47.         {
    48.             moveDirection.y -= gravity * Time.deltaTime;
    49.         }
    50.  
    51.      
    52.        
    53.         characterController.Move(moveDirection * Time.deltaTime);
    54.  
    55.        
    56.     }
    57.  
    58.    
    59. }
    60.  
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,644
    What is happening? Remember, you're close to your project and unexpected things but it's not immediately clear to others. Please describe problems.

    Thanks.
     
  10. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    @MelvMay My fault i used the wrong vid my cameras Y position is randomly going up and down
     
  11. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    what do i do?
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,644
    How could I possibly answer that? You have the project, you can debug what's going on and you can provide that information here so maybe someone can suggest further things to try. There's no magic knowledge here without information. ;)
     
  13. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    im new to this sorry, how would i troubleshoot?
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,644
    Hard to just tell you how to debug, it's a skill you have to learn either from tutorials or reading about it online.

    The main thing is to verify that what you expect to be happening, is happening. You can do this by attaching a debugger and stepping through the code or depending on what you're checking, just add "Debug.Log(xxx)" calls and look at the console to see if it's what you expect.

    The logic you have, you expect X to happen then it to do Y and/or Z. You need to verify that this is happening.
     
  15. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    11
    Ty ill try tht