I’m making Amnesia-like mechanics and currently I’m working on picking objects up. The behaviour I’m after is as follows: when I pick a book (for example) up, I want this book to be able to bump onto things that are on the scene, but the book should always try to return to the direction towards camera it had when it was picked up. After some trial and error I managed to code it, but the movement is very jittery.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Grabbable : Interactable
{
private Rigidbody rigidbody;
private bool grabbed = false;
private Vector3 targetDirection;
private Quaternion originalRotation;
private Player player;
[SerializeField] private float sensitivity = 0.05f;
void Awake()
{
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (grabbed)
{
Ray playerAim = player.PlayerCam.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f));
Vector3 nextPos = player.PlayerCam.transform.position + playerAim.direction.normalized * player.GrabbedObjectDistance;
Vector3 currentPos = transform.position;
rigidbody.velocity = (nextPos - currentPos) * 1000.0f * Time.fixedDeltaTime;
Vector3 currentDirection = (player.PlayerCam.transform.position - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation,
Quaternion.LookRotation(currentDirection) * Quaternion.Inverse(Quaternion.LookRotation(targetDirection)) * originalRotation,
sensitivity * Time.fixedDeltaTime);
}
}
public override void OnInteract(Player playerObject)
{
grabbed = true;
player = playerObject;
rigidbody.useGravity = false;
rigidbody.detectCollisions = true;
targetDirection = (player.PlayerCam.transform.position - transform.position);
originalRotation = transform.rotation;
}
public override void OnRelease()
{
grabbed = false;
rigidbody.useGravity = true;
}
}
As for rigidbody that’s on the object I’m picking up, there are no constraints, it is not set to kinematic, no kind of interpolation. Mass set to 50, both drags to 0, use gravity checked and collision type set to continuous dynamic.
The code above actually works almost as intended, but when an object bumps onto something, the movement is very jittery. Original Frictional Games titles (SOMA and first Amnesia game are the ones I’m comparing my mechanics to) have very smooth movement.
I think that I should rather calculate angular velocity for rotating object, but I’m at a loss on how to calculate it.
GIF of how this mechanic currently looks:
coolwellwornabyssiniangroundhornbill