Search Unity

Question Why can't Capsule player swing all the way around a HingeJoint while movementkey is held down?

Discussion in 'Scripting' started by ynm11, Feb 13, 2024.

  1. ynm11

    ynm11

    Joined:
    Jul 6, 2021
    Posts:
    57
    I have a project with just a rigidbody player Capsule, and a kinematic rigidbody Cube above it (which acts as a "Bar" for player to grab on to). I want Player to be able to fully swing around the bar if they 'grab' it and hold down movementkey W, which I am trying to achieve using a HingeJoint.

    Right now if player Capsule jumps and collides with the Cube 'bar' above it, a HingeJoint is created. Then the player can hold down "W" to go forward on it, which should let them do a full rotation around the bar. Instead, it stops them about 80% of the way, which is undesired behavior. Gif of this:




    Something weird of note is that if player simply presses W and releases quickly, the player is able to do a full rotation around the bar. So, there is something me holding W that makes it stop rotating at that point.

    The project only has 2 scripts in it, which are both on the rigidbody player Capsule:

    Capsule's movement controller:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Smoothy : MonoBehaviour
    8. {
    9.     Rigidbody _rigidbody = null;
    10.     public float horizontalInput;
    11.     public float verticalInput;
    12.     public Vector3 moveDirection;
    13.     public Vector3 moveDirectionRaw;
    14.     [SerializeField] float _movementMultiplier = 30.0f;
    15.     [SerializeField] Vector3 _playerCalculatedForce = Vector3.zero;
    16.  
    17.     private void Awake()
    18.     {
    19.         _rigidbody = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     public void Update()
    23.     {
    24.         horizontalInput = Input.GetAxis("Horizontal");
    25.         verticalInput = Input.GetAxis("Vertical");
    26.         moveDirection = new Vector3(horizontalInput, 0.0f, verticalInput).normalized;  
    27.         moveDirectionRaw = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical")).normalized;
    28.    
    29.         if (Input.GetKeyDown(KeyCode.Space))
    30.         {
    31.             _rigidbody.AddForce(Vector3.up * 15f, ForceMode.Impulse);
    32.         }
    33.     }
    34.  
    35.     private void FixedUpdate()
    36.     {
    37.         _playerCalculatedForce = moveDirection * _movementMultiplier;
    38.         _playerCalculatedForce *= _rigidbody.mass;
    39.         _rigidbody.AddForce(_playerCalculatedForce, ForceMode.Force);
    40.     }
    41. }
    42.  


    and the HingeJoint creation code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. public class BarLogic : MonoBehaviour
    7. {
    8.     public bool isSwinging = false;
    9.     public bool isColliding = false;
    10.     public GameObject cube;
    11.     public GameObject smoothPlayer;
    12.     RigidbodyConstraints originalConstraints;
    13.     RigidbodyConstraints barConstraints;
    14.  
    15.     private void Awake()
    16.     {
    17.         originalConstraints = GetComponent<Rigidbody>().constraints;
    18.         barConstraints = RigidbodyConstraints.None;
    19.     }
    20.  
    21.     private void OnTriggerEnter(Collider other)
    22.     {
    23.         isColliding = true;
    24.  
    25.         print("Collision!");
    26.         if (other.CompareTag("Bar"))
    27.         {
    28.             if (!this.GetComponent<HingeJoint>() && !isSwinging)
    29.             {
    30.                 //Create HingeJoint between bar and Player
    31.                 isSwinging = true;
    32.                 this.transform.rotation = Quaternion.Euler(Vector3.zero);
    33.                 this.AddComponent<HingeJoint>();
    34.                 GetComponent<Rigidbody>().constraints = barConstraints;
    35.                 this.GetComponent<HingeJoint>().autoConfigureConnectedAnchor = false;
    36.                 GetComponent<Rigidbody>().velocity = Vector3.zero;
    37.                 this.GetComponent<HingeJoint>().anchor = new Vector3(-GetComponent<Rigidbody>().position.x, 2f, 0f);
    38.                 this.GetComponent<HingeJoint>().connectedBody = other.GetComponent<Rigidbody>();
    39.             }
    40.         }
    41.         StartCoroutine(Reset());
    42.     }
    43.  
    44.     IEnumerator Reset()
    45.     {
    46.         yield return new WaitForEndOfFrame();
    47.         isColliding = false;
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         //Player releases bar
    53.         if (Input.GetKeyDown(KeyCode.R))
    54.         {
    55.             isSwinging = false;
    56.             Destroy(this.GetComponent<HingeJoint>());
    57.             GetComponent<Rigidbody>().constraints = originalConstraints;
    58.         }    
    59.     }
    60. }
    61.  
    The code above only works if the cube 'Bar' has the tag "Bar" on it.

    Any idea what might be causing this? Thank you!

    this pic is just to show the HingeJoint settings:
     
    Last edited: Feb 13, 2024
  2. ynm11

    ynm11

    Joined:
    Jul 6, 2021
    Posts:
    57
    SOLUTION: I needed to use AddRelativeForce so that it would addforce in relation to the Capsule’s forward, rather than the world’s forward!
     
    Kurt-Dekker likes this.