How would i smooth/damp rotation? [SOLVED]

So the effect i want to achieve is having a flashlights rotation lag behind my FPS camera, but i don’t know how to do this. This is all the code i have right now. I’m so stumped…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowCam : MonoBehaviour
{
    public Transform target;

    public float smoothSpeed = 0.125f;


    void FixedUpdate()
    {
        transform.position = target.position;
       
       
    }
}

Simplest way to do a follow cam is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
    public Transform target;
    public float cameraSpeed = 1f;

    void FixedUpdate()
    {
        transform.position = Vector3.MoveTowards(transform.position, target.position, cameraSpeed * Time.deltaTime);
    }
}

I’m assuming your target is using Rigidbody motion without interpolation.

however your question was about rotation, and I don’t see any rotation code in what you shared. What have you tried so far?

Actually, what I’m trying to achieve is a normal spotlight attached to the camera, with position being fixed but rotation “lagging behind”; having to catch up to the camera’s rotation. Should i add a rigid body to the spotlight?

Edit: Sorry for the nooby questions, started unity 2 weeks ago.

Oh ok. So I can help you properly can you answer some questions about your setup?

What kind of perspective does your game have? Is it first person? Is your camera attached to a player?

Yes, it it first person, with a simple character controller and a camera.

Ok so if you want the rotation to lag behind we have to make the flashlight a completely separate GameObject from the player (Not a child of the player at all). Then you can give it a script like this and point “target” at the camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
    public Transform target;
    public float rotationSpeed = 30f; // degrees per second
    void LateUpdate()
    {
        // Lock position exactly.
        transform.position = target.position;

        // Play catchup with the rotation
       transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, rotationSpeed * Time.deltaTime);
    }
}

You might want to give the script a better name too. Just attach this to the flashlight and assign your player camera as “target” in the inspector.

WOW. Thank you SO much! It works perfectly! Still though, i have one question; it’s fine if you don’t want to answer. Could i apply some kind of curve/spline to make it ease out? Thanks in advance, i won’t forget this!

Edit: Heard one way to do this is using SLERP.