Search Unity

How do I add a joint to my players hand-held lantern to make it react to movement?

Discussion in 'Physics' started by VileGoo, Aug 10, 2018.

  1. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    I'm trying to setup some physics for my players arm model so the lantern they are holding reacts to the movement by swinging around; however, I am having issues. What I've been trying to do is create some empties with a rigid body component (with isKinematic turned on) added to it and using that as an anchor for the joints. However, no matter which physic joint I add (hinge, character, configurable) the objects float around like they're weightless around the anchor point and don't react to the players movement at all. Help would be appreciated!

     
    HonoraryBob likes this.
  2. Pouya_Jigsaw

    Pouya_Jigsaw

    Joined:
    Sep 9, 2018
    Posts:
    10
    I have the same problem. it would be great if someone talk about it.
     
  3. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    I'm having the same problem, even though I have a weight (another rigidbody) attached to the bottom with gravity enabled in an attempt to get the lantern to extend downward and swing. The lantern just either remains sticking out at the same angle as the player's hand, or slowly swings all over the place when the player moves but never settles out to a vertical orientation when the player stops moving. The problem is presumably due to the joint being too rigid to allow it to swing freely, but changing the settings (or changing the type of joint) has little effect.
     
    Last edited: Feb 26, 2020
  4. NickelVfiveCENTS

    NickelVfiveCENTS

    Joined:
    Oct 10, 2021
    Posts:
    1
    I think if you adjust the 'Drag' value in the rigidbody component of the lantern, it should work as expected.
     
  5. angryfirewizard

    angryfirewizard

    Joined:
    Dec 26, 2018
    Posts:
    1
    I struggled for a few hours with this, then just decided to make my own script to control the lantern based on a first person camera view. Here it is:


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Lantern : MonoBehaviour
    4. {
    5.     public PlayerController player;
    6.     private Quaternion initialRotation;
    7.     public float maxAngle = 30f;
    8.     public float walkRotationSpeed = 4f;
    9.     public float runRotationSpeed = 6f;
    10.     public float returnSpeed = 2f;
    11.  
    12.     public float lookXMagnitude = 0.1f;
    13.     public float lookXSpeed = 1f;
    14.     private float currentRot = 0f;
    15.  
    16.     void Start()
    17.     {
    18.         initialRotation = transform.localRotation;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         WalkWobble();
    24.         MoveOnLook();
    25.     }
    26.  
    27.     private void WalkWobble() {
    28.         float rotationSpeed = walkRotationSpeed;
    29.         if (player.IsWalking || player.IsRunning) {
    30.             currentRot = Mathf.Lerp(currentRot, 1f, Time.deltaTime * returnSpeed);
    31.             // Move back and forth along the y axis of the lantern
    32.             float angle = maxAngle * Mathf.Sin(Time.time * rotationSpeed) * currentRot;
    33.             transform.localRotation = initialRotation * Quaternion.AngleAxis(angle, Vector3.up);
    34.         }
    35.         else {
    36.             // Dampen rotation until we return to original position
    37.             currentRot = Mathf.Lerp(currentRot, 0f, Time.deltaTime * returnSpeed);
    38.             float angle = maxAngle * Mathf.Sin(Time.time * rotationSpeed) * currentRot;
    39.             transform.localRotation = initialRotation * Quaternion.AngleAxis(angle, Vector3.up);
    40.         }
    41.     }
    42.  
    43.     private float curLookX = 0f;
    44.     private void MoveOnLook() {
    45.         var lookX = Input.GetAxis("Mouse X");
    46.         curLookX = Mathf.Lerp(curLookX, lookX, Time.deltaTime * lookXSpeed);
    47.         transform.Rotate(0f, curLookX * lookXMagnitude, 0f);
    48.     }
    49. }
     
    Ryiah likes this.