Created
September 29, 2020 18:35
-
-
Save maxoja/d03f1435c2bf979ccbc2d541814289fc to your computer and use it in GitHub Desktop.
Random but soothing movement Unity C# script
This file contains hidden or 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 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