Skip to content

Instantly share code, notes, and snippets.

@MarcelvanDuijnDev
Created February 14, 2022 15:03
Show Gist options
  • Save MarcelvanDuijnDev/c207c2e326ceda50791e4c680c898f2a to your computer and use it in GitHub Desktop.
Save MarcelvanDuijnDev/c207c2e326ceda50791e4c680c898f2a to your computer and use it in GitHub Desktop.
Unity Pendulum script
using UnityEngine;
public class Pendulum : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float _Speed = 1;
[SerializeField] private float _Distance = 20;
[Header("Offset")]
[SerializeField] private Vector3 _RotationOffset = Vector3.zero;
[SerializeField] private bool _SetCurrentRotationAsOffset = true;
enum AxisOptions { X, Y, Z }
[SerializeField] private AxisOptions _Axis = AxisOptions.X;
private void Start()
{
if (_SetCurrentRotationAsOffset)
_RotationOffset = transform.eulerAngles;
}
void Update()
{
float angle = _Distance * Mathf.Sin(Time.time * _Speed);
switch (_Axis)
{
case AxisOptions.X:
transform.localRotation = Quaternion.Euler(_RotationOffset.x + angle, _RotationOffset.y, _RotationOffset.z);
break;
case AxisOptions.Y:
transform.localRotation = Quaternion.Euler(_RotationOffset.x, _RotationOffset.y + angle, _RotationOffset.z);
break;
case AxisOptions.Z:
transform.localRotation = Quaternion.Euler(_RotationOffset.x, _RotationOffset.y, _RotationOffset.z + angle);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment