Skip to content

Instantly share code, notes, and snippets.

@ciwolsey
Created August 24, 2019 09:49
Show Gist options
  • Save ciwolsey/57f067b368add0881175c66a8bc1c63d to your computer and use it in GitHub Desktop.
Save ciwolsey/57f067b368add0881175c66a8bc1c63d to your computer and use it in GitHub Desktop.
Tracking
namespace Interakt.Utility {
using UnityEngine;
public class Tracking
{
// Rotates the source rigidbody towards the target transforms rotation at RotationSpeed
public static void RotateRBTowardsTransform(Rigidbody source, Transform target, float RotationSpeed)
{
var delta = target.rotation * Quaternion.Inverse(source.rotation);
delta.ToAngleAxis(out float _angle, out Vector3 _axis);
if (float.IsInfinity(_axis.x))
return;
if (_angle > 180f)
_angle -= 360f;
Vector3 angular = (RotationSpeed * Mathf.Deg2Rad * _angle / Time.deltaTime) * _axis.normalized;
source.angularVelocity = angular;
}
// Moves the source rigidbody towards the target rigidbody at MovementSpeed
public static void MoveRBtowardsTransform(Rigidbody source, Transform target, float MovementSpeed)
{
source.velocity = (target.position - source.position) * MovementSpeed;
}
// Matches target transform rotation to source transform rotation
public static void MatchTransformRotation(Transform source, Transform target)
{
source.rotation = target.rotation;
}
// Matchs target transform position to source transform position
public static void MatchTransformPosition(Transform source, Transform target)
{
source.position = target.position;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment