Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Held object updates so slow that high sensitivity wraps object behind head

Discussion in 'Physics' started by youIsArent, May 16, 2024.

  1. youIsArent

    youIsArent

    Joined:
    Dec 13, 2021
    Posts:
    14
    As the title poorly suggets, whenever I spin around really fast in this game, the object I'm holding will struggle to keep up, and if I spin arround a full 180°, the object will stay behind my head. My reference for this is amnesia, and in the gif i compare the two.


    How would i be able to increase the "update time" for my object? Or am I missing something in my code? Thanks.
     
  2. youIsArent

    youIsArent

    Joined:
    Dec 13, 2021
    Posts:
    14
    Oops, forgot the code, here it is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class GrabObjectClass
    7. {
    8.     public bool m_FreezeRotation;
    9.     public float m_PickupRange = 3f;
    10.     public float m_ThrowStrength = 50f;
    11.     public float m_distance = 3f;
    12.     public float m_maxDistanceGrab = 4f;
    13. }
    14.  
    15. [System.Serializable]
    16. public class ItemGrabClass
    17. {
    18.     public bool m_FreezeRotation;
    19.     public float m_ItemPickupRange = 2f;
    20.     public float m_ItemThrow = 45f;
    21.     public float m_ItemDistance = 1f;
    22.     public float m_ItemMaxGrab = 2.5f;
    23. }
    24.  
    25. [System.Serializable]
    26. public class DoorGrabClass
    27. {
    28.     public float m_DoorPickupRange = 2f;
    29.     public float m_DoorThrow = 10f;
    30.     public float m_DoorDistance = 2f;
    31.     public float m_DoorMaxGrab = 3f;
    32. }
    33.  
    34. [System.Serializable]
    35. public class TagsClass
    36. {
    37.     public string m_InteractTag = "Interact";
    38.     public string m_InteractItemsTag = "InteractItem";
    39.     public string m_DoorsTag = "Door";
    40. }
    41.  
    42.  
    43. public class DragRigidbodyUse : MonoBehaviour
    44. {
    45.     public GameObject playerCam;
    46.  
    47.     public string GrabButton = "Grab";
    48.     public string ThrowButton = "Throw";
    49.     public string UseButton = "Use";
    50.     public GrabObjectClass ObjectGrab = new GrabObjectClass();
    51.     public ItemGrabClass ItemGrab = new ItemGrabClass();
    52.     public DoorGrabClass DoorGrab = new DoorGrabClass();
    53.     public TagsClass Tags = new TagsClass();
    54.  
    55.     private float PickupRange = 3f;
    56.     private float ThrowStrength = 50f;
    57.     private float distance = 3f;
    58.     private float maxDistanceGrab = 4f;
    59.  
    60.     private Ray playerAim;
    61.     private GameObject objectHeld;
    62.     private bool isObjectHeld;
    63.     private bool tryPickupObject;
    64.  
    65.     void Start()
    66.     {
    67.         isObjectHeld = false;
    68.         tryPickupObject = false;
    69.         objectHeld = null;
    70.     }
    71.  
    72.     void FixedUpdate()
    73.     {
    74.         if (Input.GetButton(GrabButton))
    75.         {
    76.             if (!isObjectHeld)
    77.             {
    78.                 tryPickObject();
    79.                 tryPickupObject = true;
    80.             }
    81.             else
    82.             {
    83.                 holdObject();
    84.             }
    85.         }
    86.         else if (isObjectHeld)
    87.         {
    88.             DropObject();
    89.         }
    90.  
    91.         if (Input.GetButton(ThrowButton) && isObjectHeld)
    92.         {
    93.             isObjectHeld = false;
    94.             objectHeld.GetComponent<Rigidbody>().useGravity = true;
    95.             ThrowObject();
    96.         }
    97.  
    98.         if (Input.GetButton(UseButton))
    99.         {
    100.             isObjectHeld = false;
    101.             tryPickObject();
    102.             tryPickupObject = false;
    103.             Use();
    104.         }
    105.     }
    106.  
    107.     private void tryPickObject()
    108.     {
    109.         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    110.         RaycastHit hit;
    111.  
    112.         if (Physics.Raycast(playerAim, out hit, PickupRange))
    113.         {
    114.             objectHeld = hit.collider.gameObject;
    115.             if (hit.collider.tag == Tags.m_InteractTag && tryPickupObject)
    116.             {
    117.                 isObjectHeld = true;
    118.                 objectHeld.GetComponent<Rigidbody>().useGravity = false;
    119.                 if (ObjectGrab.m_FreezeRotation)
    120.                 {
    121.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
    122.                 }
    123.                 if (ObjectGrab.m_FreezeRotation == false)
    124.                 {
    125.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    126.                 }
    127.                 /**/
    128.                 PickupRange = ObjectGrab.m_PickupRange;
    129.                 ThrowStrength = ObjectGrab.m_ThrowStrength;
    130.                 distance = ObjectGrab.m_distance;
    131.                 maxDistanceGrab = ObjectGrab.m_maxDistanceGrab;
    132.             }
    133.             if (hit.collider.tag == Tags.m_InteractItemsTag && tryPickupObject)
    134.             {
    135.                 isObjectHeld = true;
    136.                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
    137.                 if (ItemGrab.m_FreezeRotation)
    138.                 {
    139.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
    140.                 }
    141.                 if (ItemGrab.m_FreezeRotation == false)
    142.                 {
    143.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    144.                 }
    145.                 /**/
    146.                 PickupRange = ItemGrab.m_ItemPickupRange;
    147.                 ThrowStrength = ItemGrab.m_ItemThrow;
    148.                 distance = ItemGrab.m_ItemDistance;
    149.                 maxDistanceGrab = ItemGrab.m_ItemMaxGrab;
    150.             }
    151.             if (hit.collider.tag == Tags.m_DoorsTag && tryPickupObject)
    152.             {
    153.                 isObjectHeld = true;
    154.                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
    155.                 objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    156.                 /**/
    157.                 PickupRange = DoorGrab.m_DoorPickupRange;
    158.                 ThrowStrength = DoorGrab.m_DoorThrow;
    159.                 distance = DoorGrab.m_DoorDistance;
    160.                 maxDistanceGrab = DoorGrab.m_DoorMaxGrab;
    161.             }
    162.         }
    163.     }
    164.  
    165.     private void holdObject()
    166.     {
    167.         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    168.  
    169.         Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
    170.         Vector3 currPos = objectHeld.transform.position;
    171.  
    172.         objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 10;
    173.  
    174.         if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
    175.         {
    176.             DropObject();
    177.         }
    178.     }
    179.  
    180.     private void DropObject()
    181.     {
    182.         isObjectHeld = false;
    183.         tryPickupObject = false;
    184.         objectHeld.GetComponent<Rigidbody>().useGravity = true;
    185.         objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    186.         objectHeld = null;
    187.     }
    188.  
    189.     private void ThrowObject()
    190.     {
    191.         objectHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
    192.         objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    193.         objectHeld = null;
    194.     }
    195.  
    196.     private void Use()
    197.     {
    198.         objectHeld.SendMessage("UseObject", SendMessageOptions.DontRequireReceiver); //Every script attached to the PickupObject that has a UseObject function will be called.
    199.         objectHeld = null;
    200.     }
    201. }
    202.  
     
  3. youIsArent

    youIsArent

    Joined:
    Dec 13, 2021
    Posts:
    14
    Alright, for anyone wondering. The jitteryness is caused by interpolation set to none, and the lagging behind is because there are missed frames or something. I understood it at one point, implemeneted it, went to sleep and now barely understand my own code. here is everything i used and my new, working script.

    explaination of interpolation
    https://gamedev.stackexchange.com/questions/105048/smooth-camera-follow/105113#105113

    explaination of Time.Deltatime (a bit)
    https://gamedev.stackexchange.com/q...o-manually-simulate-physics-properly-on-unity

    here is the new code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class GrabObjectClass
    7. {
    8.     public bool m_FreezeRotation;
    9.     public float m_PickupRange = 3f;
    10.     public float m_ThrowStrength = 50f;
    11.     public float m_distance = 3f;
    12.     public float m_maxDistanceGrab = 4f;
    13. }
    14.  
    15. [System.Serializable]
    16. public class ItemGrabClass
    17. {
    18.     public bool m_FreezeRotation;
    19.     public float m_ItemPickupRange = 2f;
    20.     public float m_ItemThrow = 45f;
    21.     public float m_ItemDistance = 1f;
    22.     public float m_ItemMaxGrab = 2.5f;
    23. }
    24.  
    25. [System.Serializable]
    26. public class DoorGrabClass
    27. {
    28.     public float m_DoorPickupRange = 2f;
    29.     public float m_DoorThrow = 10f;
    30.     public float m_DoorDistance = 2f;
    31.     public float m_DoorMaxGrab = 3f;
    32. }
    33.  
    34. [System.Serializable]
    35. public class TagsClass
    36. {
    37.     public string m_InteractTag = "Interact";
    38.     public string m_InteractItemsTag = "InteractItem";
    39.     public string m_DoorsTag = "Door";
    40. }
    41.  
    42.  
    43. public class DragRigidbodyUse : MonoBehaviour
    44. {
    45.     public GameObject playerCam;
    46.  
    47.     public string GrabButton = "Grab";
    48.     public string ThrowButton = "Throw";
    49.     public string UseButton = "Use";
    50.     public GrabObjectClass ObjectGrab = new GrabObjectClass();
    51.     public ItemGrabClass ItemGrab = new ItemGrabClass();
    52.     public DoorGrabClass DoorGrab = new DoorGrabClass();
    53.     public TagsClass Tags = new TagsClass();
    54.  
    55.     private float PickupRange = 3f;
    56.     private float ThrowStrength = 50f;
    57.     private float distance = 3f;
    58.     private float maxDistanceGrab = 4f;
    59.  
    60.     private Ray playerAim;
    61.     private GameObject objectHeld;
    62.     private bool isObjectHeld;
    63.     private bool tryPickupObject;
    64.  
    65.     void Start()
    66.     {
    67.         isObjectHeld = false;
    68.         tryPickupObject = false;
    69.         objectHeld = null;
    70.     }
    71.  
    72.     void FixedUpdate()
    73.     {
    74.         if (Input.GetButton(GrabButton))
    75.         {
    76.             if (!isObjectHeld)
    77.             {
    78.                 tryPickObject();
    79.                 tryPickupObject = true;
    80.             }
    81.             else
    82.             {
    83.                 holdObject();
    84.             }
    85.         }
    86.         else if (isObjectHeld)
    87.         {
    88.             DropObject();
    89.         }
    90.  
    91.         if (Input.GetButton(ThrowButton) && isObjectHeld)
    92.         {
    93.             isObjectHeld = false;
    94.             objectHeld.GetComponent<Rigidbody>().useGravity = true;
    95.             ThrowObject();
    96.         }
    97.  
    98.         if (Input.GetButton(UseButton))
    99.         {
    100.             isObjectHeld = false;
    101.             tryPickObject();
    102.             tryPickupObject = false;
    103.             Use();
    104.         }
    105.     }
    106.  
    107.     private void tryPickObject()
    108.     {
    109.         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    110.         RaycastHit hit;
    111.  
    112.         if (Physics.Raycast(playerAim, out hit, PickupRange))
    113.         {
    114.             objectHeld = hit.collider.gameObject;
    115.             if (hit.collider.tag == Tags.m_InteractTag && tryPickupObject)
    116.             {
    117.                 isObjectHeld = true;
    118.                 objectHeld.GetComponent<Rigidbody>().useGravity = false;
    119.                 if (ObjectGrab.m_FreezeRotation)
    120.                 {
    121.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
    122.                 }
    123.                 if (ObjectGrab.m_FreezeRotation == false)
    124.                 {
    125.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    126.                 }
    127.                 /**/
    128.                 PickupRange = ObjectGrab.m_PickupRange;
    129.                 ThrowStrength = ObjectGrab.m_ThrowStrength;
    130.                 distance = ObjectGrab.m_distance;
    131.                 maxDistanceGrab = ObjectGrab.m_maxDistanceGrab;
    132.             }
    133.             if (hit.collider.tag == Tags.m_InteractItemsTag && tryPickupObject)
    134.             {
    135.                 isObjectHeld = true;
    136.                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
    137.                 if (ItemGrab.m_FreezeRotation)
    138.                 {
    139.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
    140.                 }
    141.                 if (ItemGrab.m_FreezeRotation == false)
    142.                 {
    143.                     objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    144.                 }
    145.                 /**/
    146.                 PickupRange = ItemGrab.m_ItemPickupRange;
    147.                 ThrowStrength = ItemGrab.m_ItemThrow;
    148.                 distance = ItemGrab.m_ItemDistance;
    149.                 maxDistanceGrab = ItemGrab.m_ItemMaxGrab;
    150.             }
    151.             if (hit.collider.tag == Tags.m_DoorsTag && tryPickupObject)
    152.             {
    153.                 isObjectHeld = true;
    154.                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
    155.                 objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    156.                 /**/
    157.                 PickupRange = DoorGrab.m_DoorPickupRange;
    158.                 ThrowStrength = DoorGrab.m_DoorThrow;
    159.                 distance = DoorGrab.m_DoorDistance;
    160.                 maxDistanceGrab = DoorGrab.m_DoorMaxGrab;
    161.             }
    162.         }
    163.     }
    164.  
    165.     private void holdObject()
    166.     {
    167.         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    168.        Vector3 currPos = objectHeld.transform.position;
    169.         Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
    170.         objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 1/Time.deltaTime;
    171.  
    172.         if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
    173.         {
    174.             DropObject();
    175.         }
    176.     }
    177.  
    178.    
    179.     private void DropObject()
    180.     {
    181.         isObjectHeld = false;
    182.         tryPickupObject = false;
    183.         objectHeld.GetComponent<Rigidbody>().useGravity = true;
    184.         objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    185.         objectHeld = null;
    186.     }
    187.  
    188.     private void ThrowObject()
    189.     {
    190.         objectHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
    191.         objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
    192.         objectHeld = null;
    193.     }
    194.  
    195.     private void Use()
    196.     {
    197.         objectHeld.SendMessage("UseObject", SendMessageOptions.DontRequireReceiver); //Every script attached to the PickupObject that has a UseObject function will be called.
    198.         objectHeld = null;
    199.     }
    200. }
    201.  
    202.  
    203.  
    be aware that if the object is clicked, it will fly into the air, I assume it is because when you pick something up, it gets a certain amount of acceleration as it moves from the max pick up distance to the distance from your camera when it's picked up, but this also seems to work as intended. Imagine a pen up only to let go immediatly, it would hit the ceiling.