Smooth movement of the object behind the camera

Hello.

I’m new to Unity

I’m new to Unity and would like to know how I can make an object behind the camera move so that it looks like it’s being held in my hands. I know you can just throw an object into the camera, but that’s not what I need.I’ve already tried to write a script.

public class CameraScript : MonoBehaviour
{
public Transform ObjectPosition;
private Vector3 destination;

// Update is called once per frame
void Update()
{
    destination = ObjectPosition.position + new Vector3(0, -0.2f, 0.5f);

    transform.position = Vector3.Lerp(transform.position, destination, 0.1f);
    transform.rotation = Quaternion.Lerp(transform.rotation, ObjectPosition.rotation, 0.1f);
}

}

If you want to move an object smoothly, use a tweening package such as iTween or DOTween or LeanTween.

If you want to control a camera, use Cinemachine.

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

You might be able to modify this to fit your needs:

using UnityEngine;
public class FollowCursor : MonoBehaviour // Make an object follow the mouse cursor 
{
Vector3 velocity;
    void Update()
    {
        Vector3 p=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,10));
        transform.position+=velocity*Time.deltaTime;
        velocity+=((p-transform.position)*40)*Time.deltaTime; // move speed
        velocity-=(velocity*5f)*Time.deltaTime; // damper. increase this reduce wobble
    }
}