I have a GameObject that is placed in front of the camera, so whenever the player picks up an object, it will be placed in the GameObejct's location. but whenever I move while I'm picking up an object, the object shakes. How would I prevent that from happening?
private void FixedUpdate()
{
if (currentlyPickedUpObject != null)
{
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
}
}
if (PickingUp)
{
if (currentlyPickedUpObject == null)
{
if (lookObject != null)
{
PickupObject();
if (lookObject.CompareTag("TargetObj") && !targetObjectsList.Contains(lookObject.gameObject))
{
if (aSource)
{
aSource.Play();
}
targetObjectsList.Add(lookObject.gameObject);
if (targetObjectsList.Count == targetObjects.Length)
{
winUI.SetActive(true);
Time.timeScale = 0f;
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
//Time.timeScale = 1f;
}
}
}
}
else
{
// pickupRB.transform.position = PickupParent.position;
BreakConnection();
HoldingItemIcon.SetActive(false);
InteractIcon.SetActive(false);
}
}
PickingUp = false;
public void BreakConnection()
{
pickupRB.constraints = RigidbodyConstraints.None;
currentlyPickedUpObject = null;
lookObject = null;
physicsObject.pickedUp = false;
currentDist = 0;
pickupRB.useGravity = true;
}
public void PickupObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
pickupRB.isKinematic = true;
// pickupRB.transform.position = PickupParent.position;
pickupRB.transform.parent = PickupParent.transform;
//StartCoroutine(physicsObject.PickUp());
}
and here is the inspector of pickable objects:

and here is the code attached to the pickable objects:
public class PhysicsObjects : MonoBehaviour
{
public float waitOnPickup = 0.1f;
public float breakForce = 35f;
[HideInInspector] public bool pickedUp = false;
[HideInInspector] public ThePlayerInteractions playerInteractions;
private void OnCollisionEnter(Collision collision)
{
if (pickedUp)
{
if (collision.relativeVelocity.magnitude > breakForce)
{
playerInteractions.BreakConnection();
}
}
}
//this is used to prevent the connection from breaking when you just picked up the object as it sometimes fires a collision with the ground or whatever it is touching
public IEnumerator PickUp()
{
yield return new WaitForSecondsRealtime(waitOnPickup);
pickedUp = true;
}
}
other than shaking, the picked-up objects lose their colliders for some reason, they go through any objects they hit. what is the best way to avoid these issues when the objects are being held?