Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How would i smooth/damp rotation? [SOLVED]

Discussion in 'Scripting' started by T0byCat, Aug 17, 2020.

  1. T0byCat

    T0byCat

    Joined:
    Aug 17, 2020
    Posts:
    14
    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...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowCam : MonoBehaviour
    6. {
    7.     public Transform target;
    8.  
    9.     public float smoothSpeed = 0.125f;
    10.  
    11.  
    12.     void FixedUpdate()
    13.     {
    14.         transform.position = target.position;
    15.        
    16.        
    17.     }
    18. }
    19.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Simplest way to do a follow cam is this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class FollowCam : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public float cameraSpeed = 1f;
    8.  
    9.     void FixedUpdate()
    10.     {
    11.         transform.position = Vector3.MoveTowards(transform.position, target.position, cameraSpeed * Time.deltaTime);
    12.     }
    13. }
    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?
     
  3. T0byCat

    T0byCat

    Joined:
    Aug 17, 2020
    Posts:
    14
    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.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    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?
     
  5. T0byCat

    T0byCat

    Joined:
    Aug 17, 2020
    Posts:
    14
    Yes, it it first person, with a simple character controller and a camera.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class FollowCam : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public float rotationSpeed = 30f; // degrees per second
    8.     void LateUpdate()
    9.     {
    10.         // Lock position exactly.
    11.         transform.position = target.position;
    12.  
    13.         // Play catchup with the rotation
    14.        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, rotationSpeed * Time.deltaTime);
    15.     }
    16. }
    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.
     
  7. T0byCat

    T0byCat

    Joined:
    Aug 17, 2020
    Posts:
    14
    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.
     
    Last edited: Aug 17, 2020