Skip to content

Instantly share code, notes, and snippets.

@maxoja
Created September 29, 2020 18:35
Show Gist options
  • Save maxoja/d03f1435c2bf979ccbc2d541814289fc to your computer and use it in GitHub Desktop.
Save maxoja/d03f1435c2bf979ccbc2d541814289fc to your computer and use it in GitHub Desktop.
Random but soothing movement Unity C# script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinAround : MonoBehaviour
{
public float rotX, rotY, rotZ;
public float moveScale;
public Vector3 changeSpeed = Vector3.one;
private Rigidbody rb;
private Vector3 progression;
private void Awake() {
this.rb = GetComponent<Rigidbody>();
}
private void UpdateAngularVelocity() {
this.rb.angularVelocity = new Vector3(
rotX*Mathf.Sin(progression.x),
rotY*Mathf.Cos(progression.y),
rotZ*(Mathf.Cos(progression.z)+Mathf.Sin(progression.z))
);
}
void UpdatePosition() {
Vector3 currentPosition = transform.position;
Vector3 magnetPosition = moveScale * new Vector3(
rotX*Mathf.Sin(progression.x),
rotY*Mathf.Cos(progression.y),
rotZ*(Mathf.Cos(progression.z)+Mathf.Sin(progression.z))
);
Vector3 newPosition = Vector3.Lerp(currentPosition, magnetPosition, Time.deltaTime*2);
transform.position = newPosition;
}
void Update()
{
UpdateAngularVelocity();
UpdatePosition();
progression += Time.deltaTime * changeSpeed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment