Search Unity

Question Unintended axis snapping and unknown force on Impulse reaction behavior.

Discussion in 'Physics' started by Thiesl137, Nov 8, 2023.

  1. Thiesl137

    Thiesl137

    Joined:
    May 19, 2023
    Posts:
    2
    Hello Community,

    UNITY VERSION 2022.3.11f1
    3D URP project

    I am working on a simple player controller script and I am experiencing difficulty determining why the velocity is behaving in unintended ways.

    Two Issues:

    1 - When I rotate the player Rigidbody while moving forward - the velocity/direction snaps to the worlds x-axis.

    The yellow line is Velocity
    The red line is acting forces (pushes based on key (W) input and direction with mouse input)

    Video Demo : https://clipchamp.com/watch/kpePTVpR73s

    Question - Why would a velocity snap to a world axis like that and how can I start to resolve?


    2 - When I hit the space key to apply a impulse force in the y direction there is a force being applied in the z world axis direction that is pushing the Rigidbody and I have spend a lot of time commenting out other acting forces and looking through the UI to find what might be acting.

    again

    The yellow line is Velocity
    The red line is acting forces (pushes based on key (W) input and direction with mouse input)

    Video Demo : https://clipchamp.com/watch/phAYs1v4ULU

    Question - Why would the velocity be immediately angled to the z direction when the impulse is set world y and why is there a deflection at the end of the arc path as though the rigidbody is colliding against an invisible wall?

    Asset structure

    CAMERA
    Parent - CameraHolder - MoveCamera Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveCamera : MonoBehaviour
    6. {
    7.     public Transform cameraPosition;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         transform.position = cameraPosition.position;
    13.     }
    14. }
    15.  
    Child - PlayerCam (main camera) - PlayerCam script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerCam : MonoBehaviour
    6. {
    7.  
    8.     public float sensX;
    9.     public float sensY;
    10.  
    11.     public Transform orientation;
    12.  
    13.     float xRotation;
    14.     float yRotation;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         Cursor.lockState = CursorLockMode.Locked;
    21.         Cursor.visible = false;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         // Get Mouse Input
    28.  
    29.         float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
    30.         float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;
    31.  
    32.         yRotation += mouseX;
    33.         xRotation -= mouseY;
    34.  
    35.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    36.  
    37.  
    38.  
    39.         transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
    40.         orientation.rotation = Quaternion.Euler(0, yRotation, 0);
    41.     }
    42. }
    43.  
    PLAYER
    Parent - Player - Rigidbody - Player Movement Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     [Header("Movement")]
    8.     public float moveSpeed;
    9.  
    10.     float horizontalInput;
    11.     float verticalInput;
    12.  
    13.     Vector3 moveDirection;
    14.  
    15.     Rigidbody rb;
    16.  
    17.     public float groundDrag;
    18.  
    19.     public float jumpForce;
    20.     public float jumpCooldown;
    21.     public float airMultiplier;
    22.     bool readyToJump = true;
    23.  
    24.     [Header("Keybinds")]
    25.     public KeyCode jumpKey = KeyCode.Space;
    26.  
    27.     [Header("Ground Check")]
    28.     public float playerHeight;
    29.     public LayerMask whatIsGround;
    30.     public Transform orientation;
    31.     bool grounded;
    32.  
    33.     // Start is called before the first frame update
    34.     void Start()
    35.     {
    36.         rb = GetComponent<Rigidbody>();
    37.         rb.freezeRotation = true;
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.  
    44.     }
    45.  
    46.     private void FixedUpdate()
    47.     {
    48.         MovePlayer();
    49.  
    50.         Debug.DrawRay(rb.position, rb.velocity, Color.yellow);
    51.         Debug.DrawRay(rb.position, rb.GetAccumulatedForce(), Color.red);
    52.  
    53.         // Ground Check
    54.         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
    55.  
    56.         MyInput();
    57.         SpeedControl();
    58.  
    59.         // Handle drag
    60.         if (grounded)
    61.             rb.drag = groundDrag;
    62.         else
    63.             rb.drag = 0;
    64.     }
    65.     private void MyInput()
    66.     {
    67.         horizontalInput = Input.GetAxis("Horizontal");
    68.         verticalInput = Input.GetAxis("Vertical");
    69.  
    70.         if (Input.GetKey(jumpKey) && readyToJump && grounded)
    71.         {
    72.             readyToJump = false;
    73.  
    74.             Jump();
    75.  
    76.             Invoke(nameof(ResetJump), jumpCooldown);
    77.         }
    78.     }
    79.  
    80.     private void MovePlayer()
    81.     {
    82.         // Calculate movement direction
    83.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    84.         Debug.Log("Orientation --- " + orientation.position);
    85.         Debug.Log("Orientation --- " + orientation.forward);
    86.         Debug.DrawLine(rb.position, moveDirection, Color.green);
    87.  
    88.         // On Ground
    89.         if (grounded)
    90.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    91.         else if (!grounded)
    92.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    93.     }
    94.    
    95.     private void SpeedControl()
    96.     {
    97.         Vector3 flatVelocity = new Vector3(rb.velocity.x, 0f, rb.velocity.y);
    98.  
    99.         // Limit velocity if needed
    100.         if(flatVelocity.magnitude > moveSpeed)
    101.         {
    102.             Vector3 limitedVelocity = flatVelocity.normalized * moveSpeed;
    103.             rb.velocity = new Vector3(limitedVelocity.x, rb.velocity.y, limitedVelocity.z);
    104.         }
    105.     }
    106.  
    107.     private void Jump()
    108.     {
    109.         // Reset Y Velocity
    110.         rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    111.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    112.     }
    113.  
    114.     private void ResetJump()
    115.     {
    116.         readyToJump = true;
    117.     }
    118. }
    119.  
    Child - PlayerObj - Capsule Collider
    Child - Orientation - just transform for orientation
    Child - CameraPos - just transform for position
     
  2. Thiesl137

    Thiesl137

    Joined:
    May 19, 2023
    Posts:
    2
    Edit:
    Code (CSharp):
    1.  
    2.         Debug.DrawRay(rb.position, rb.velocity, Color.yellow);
    3.         Debug.DrawRay(rb.position, rb.GetAccumulatedForce(), Color.red);
    4.         // Ground Check
    5.         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
    6.         MyInput();
    7.         SpeedControl();
    8.         // Handle drag
    9.         if (grounded)
    10.             rb.drag = groundDrag;
    11.         else
    12.             rb.drag = 0;
    13.  
    is in the
    Update()
    method, not
    fixedUpdate()
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,425
    Know that you can edit your original post.