Created
November 23, 2022 17:10
-
-
Save cdiggins/2eeb1b596b274365e7b63342bf71d6d4 to your computer and use it in GitHub Desktop.
Unity Interpolation demo script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
[ExecuteAlways] | |
public class PMoveTowards : MonoBehaviour | |
{ | |
public float Amount; | |
public bool Clamp; | |
public Transform Target; | |
public bool Position = true; | |
public Vector3 StartPosition; | |
public Vector3 EndPosition; | |
public bool Rotation = true; | |
public Quaternion StartRotation; | |
public Quaternion EndRotation; | |
public bool Scale = true; | |
public Vector3 StartScale; | |
public Vector3 EndScale; | |
public bool Reset; | |
public void OnEnable() | |
{ | |
Recompute(); | |
} | |
public void Recompute() | |
{ | |
StartPosition = transform.position; | |
EndPosition = Target.position; | |
StartRotation = transform.rotation; | |
EndRotation = Target.rotation; | |
StartScale = transform.localScale; | |
EndScale = Target.localScale; | |
} | |
public void Update() | |
{ | |
if (Reset) | |
{ | |
Recompute(); | |
Amount = 0; | |
Reset = false; | |
} | |
if (Clamp) | |
{ | |
if (Position) transform.position = Vector3.Lerp(StartPosition, EndPosition, Amount); | |
if (Rotation) transform.rotation = Quaternion.Slerp(StartRotation, EndRotation, Amount); | |
if (Scale) transform.localScale = Vector3.Lerp(StartScale, EndScale, Amount); | |
} | |
else | |
{ | |
if (Position) transform.position = Vector3.LerpUnclamped(StartPosition, EndPosition, Amount); | |
if (Rotation) transform.rotation = Quaternion.SlerpUnclamped(StartRotation, EndRotation, Amount); | |
if (Scale) transform.localScale = Vector3.LerpUnclamped(StartScale, EndScale, Amount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment