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

Resolved How can i change this code to only smooth on the y axis

Discussion in 'Scripting' started by lucaskargreen, Sep 11, 2020.

  1. lucaskargreen

    lucaskargreen

    Joined:
    Sep 10, 2020
    Posts:
    21
    This script perfectly does what I want but on all axis. I'm pretty new and I can't seem to get it to do it only on the y-axis

    To clarify I want it to follow on all the other axis as well but instant and I want to smoothing on the y-axis

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollowTarget : MonoBehaviour
    4. {
    5.     public Transform target;
    6.     public float smootSpeed = 10f;
    7.  
    8.     private Vector3 velocity = Vector3.zero;
    9.  
    10.     void LateUpdate()
    11.     {
    12.         Vector3 desiredPostion = target.position;
    13.         Vector3 smoothedPosition = Vector3.SmoothDamp (transform.position, desiredPostion, ref velocity, smootSpeed*Time.deltaTime);
    14.         transform.position = smoothedPosition;
    15.  
    16.     }
    17. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You could take the XZ component from target.position and the Y component from smoothedPosition and create a new vector based on that, then set your transform.position to that. This should directly place you at the target XZ and only smooth Y.
     
    lucaskargreen likes this.
  3. lucaskargreen

    lucaskargreen

    Joined:
    Sep 10, 2020
    Posts:
    21
    Ended up with:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollowTarget : MonoBehaviour
    4. {
    5.     public Transform target;
    6.     public float smoothSpeed = 25f;
    7.  
    8.     private Vector3 velocity = Vector3.zero;
    9.  
    10.     void LateUpdate()
    11.     {
    12.         Vector3 desiredPostion = target.position;
    13.         Vector3 smoothedPosition = Vector3.SmoothDamp (transform.position, desiredPostion, ref velocity, smoothSpeed * Time.deltaTime);
    14.         transform.position = new Vector3(desiredPostion.x, smoothedPosition.y, desiredPostion.z);
    15.  
    16.     }
    17. }
    And it works flawlessly, Thanks a lot