0

I have a function that lerps an object in front of the camera to "pickup/hold" said object. However, while holding the object, if I hit it against another object a certain way, the held object will rotate in an unexpected way. I am not changing the rotation of the object anywhere in my script. What is going on and how could I fix this so that it rotates properly? This is what it looks like

void Grab()
{
    if (musketState == MusketState.Grab) 
    {
        objectToGrab = hit.transform.gameObject;
        Rigidbody objectRigidbody = objectToGrab.GetComponent<Rigidbody> ();
        Vector3 desiredPosition = mainCamera.transform.position + mainCamera.transform.forward * grabDistance;
        Vector3 smoothedPosition = Vector3.Lerp (objectToGrab.transform.position, desiredPosition, smoothSpeed * Time.deltaTime);

        isGrabbingObject = true;
        objectRigidbody.useGravity = false;
        objectRigidbody.transform.position = smoothedPosition;
    }
}
  • Seems like you are using rigidbody. Which enables physics interaction to an object. So when an object with rigidbody "collides" with another object which has a collider, it will rotate/move. You can freeze rotation on axis that you don't want to rotate from rigidbody properties – Thalthanas Feb 19 '18 at 08:15
  • @Thalthanas I know that it will rotate when colliding with Rigidbody. I want that. But sometimes it will rotate backwards or in an unexpected direction. [Like this](https://imgur.com/a/kw5m2) – LazyBriefcase Feb 19 '18 at 09:22
  • The rotation you experience is the rotation calculated by Unity's physics system. How would you like it to rotate instead of what it does now? Possible solutions depend on what you expect to be the outcome. – Doh09 Feb 19 '18 at 09:42

0 Answers0