Last active
July 2, 2022 22:27
-
-
Save awhiskin/50fdc90e233281c2de45a62dae8256e0 to your computer and use it in GitHub Desktop.
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class FakeBroomPhysics : MonoBehaviour | |
{ | |
[SerializeField] | |
// Assign the Transform that contains the broom head model | |
Transform broomHead = default; | |
[SerializeField] | |
// Smooths out the rotation to reduce jitter | |
float smoothing = 8f; | |
private Vector3 lastPosition = Vector3.zero; | |
// This works fine but you may want to re-write this to remove Y-axis value, so it will only track X-axis and Z-axis movement | |
private Vector3 velocity { get { return broomHead != null ? lastPosition - broomHead.transform.position : Vector3.zero; } } | |
private void Update() | |
{ | |
if (broomHead == null) { return; } | |
if (velocity != Vector3.zero) | |
broomHead.transform.rotation = Quaternion.Slerp(broomHead.transform.rotation, Quaternion.LookRotation(-velocity), Time.deltaTime * smoothing); | |
lastPosition = broomHead.transform.position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment