Skip to content

Instantly share code, notes, and snippets.

@AldeRoberge
Created May 21, 2025 20:39
Show Gist options
  • Save AldeRoberge/1a061e6a4605f4654eceefbda97d87f3 to your computer and use it in GitHub Desktop.
Save AldeRoberge/1a061e6a4605f4654eceefbda97d87f3 to your computer and use it in GitHub Desktop.
Allows to rotate an object around the target (a VR camera in my case).
using Sirenix.OdinInspector;
using UnityEngine;
public class RotationFollowSmooth : MonoBehaviour
{
[SerializeField, BoxGroup("References"), Required]
private Transform _target;
[BoxGroup("Settings")]
[SerializeField, BoxGroup("Settings/Rotation"), Required]
private bool _followRotation = true;
[SerializeField, BoxGroup("Settings/Rotation"), Required, EnableIf("_followRotation")]
private bool _smoothRotation = true;
[SerializeField, BoxGroup("Settings/Rotation"), Required, ShowIf("_smoothRotation")]
private float _rotationSpeed = 5f;
[SerializeField, BoxGroup("Settings/Rotation"), Required]
private bool _mirrorRotation = true;
[BoxGroup("Settings")]
[SerializeField, BoxGroup("Settings/Position"), Required]
private bool _smoothPosition = true;
[SerializeField, BoxGroup("Settings/Position"), Required, ShowIf("_smoothPosition")]
private float _positionSpeed = 5f;
private Vector3 _initialOffset;
private void Start()
{
if (_target == null)
{
Debug.LogError("Target not set for RotationFollowSmooth.");
enabled = false;
return;
}
_initialOffset = transform.position - _target.position;
}
private void Update()
{
if (!_followRotation) return;
// Rotate the initial offset by target's Y rotation
Quaternion rotationOnlyY = Quaternion.Euler(0f, _target.eulerAngles.y, 0f);
Vector3 desiredPosition = _target.position + rotationOnlyY * _initialOffset;
// Apply position (smooth or instant)
if (_smoothPosition)
{
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime * _positionSpeed);
}
else
{
transform.position = desiredPosition;
}
// Calculate the rotation to look at the target
Quaternion targetRotation = Quaternion.LookRotation(_target.position - transform.position);
// Apply mirroring if needed
if (_mirrorRotation)
{
targetRotation *= Quaternion.Euler(0f, 180f, 0f);
}
// Apply rotation (smooth or instant)
if (_smoothRotation)
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * _rotationSpeed);
}
else
{
transform.rotation = targetRotation;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment