Skip to content

Instantly share code, notes, and snippets.

@jeffcrouse
Created January 29, 2018 23:34
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A handy script for making a Unity GameObject rotate and "wobble" with exposed parameters for speed and amount.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
* Lessons:
* 1. Scripts are mini-programs that operate on a GameObject in your scene.
* 2. How to get a component of the GameObject this script is attached to?
* 3. How to modify that component over time.
* 4. FIX IN CLASS: There is actually a shortcut to get the transform of the GameObject.
**/
public class Hover : MonoBehaviour {
// Public variables show up in the Inspector
public Vector3 RotateSpeed = new Vector3 (10.0F, 10.0F, 10.0F);
public Vector3 WobbleAmount = new Vector3 (0.1F, 0.1F, 0.1F);
public Vector3 WobbleSpeed = new Vector3 (0.5F, 0.5F, 0.5F);
// Private variables do not show up in the Inspector
private Transform tr;
private Vector3 BasePosition;
private Vector3 NoiseIndex = new Vector3();
// Use this for initialization
void Start () {
// https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
tr = GetComponent ("Transform") as Transform;
BasePosition = tr.position;
NoiseIndex.x = Random.value;
NoiseIndex.y = Random.value;
NoiseIndex.z = Random.value;
}
// Update is called once per frame
void Update () {
// 1. ROTATE
// Rotate the cube by RotateSpeed, multiplied by the fraction of a second that has passed.
// In other words, we want to rotate by the full amount over 1 second
tr.Rotate (Time.deltaTime * RotateSpeed);
// 2. WOBBLE
// Calculate how much to offset the cube from it's base position using PerlinNoise
Vector3 offset = new Vector3 ();
offset.x = Mathf.PerlinNoise (NoiseIndex.x, 0) - 0.5F;
offset.y = Mathf.PerlinNoise (NoiseIndex.y, 0) - 0.5F;
offset.z = Mathf.PerlinNoise (NoiseIndex.z, 0) - 0.5F;
offset.Scale (WobbleAmount);
// Set the position to the BasePosition plus the offset
transform.position = BasePosition + offset;
// Increment the NoiseIndex so that we get a new Noise value next time.
NoiseIndex += WobbleSpeed * Time.deltaTime;
}
}
@YaBoyCJ
Copy link

YaBoyCJ commented Dec 5, 2020

you dont know how much this helped me

@notpresleybutelvis
Copy link

@jeffcrouse this is awesome, thank you for writing this

@Baccanno
Copy link

Will try thanks !

@danielbierwirth-unity
Copy link

thanks man, nice work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment