Skip to content

Instantly share code, notes, and snippets.

@kleberandrade
Created July 17, 2014 13:27
Show Gist options
  • Save kleberandrade/d5ef204beb75268c31e8 to your computer and use it in GitHub Desktop.
Save kleberandrade/d5ef204beb75268c31e8 to your computer and use it in GitHub Desktop.
Componente para Unity 3D, que faz objetos oscilarem dentro do jogo, seja sua posição, rotação ou escala.
using UnityEngine;
using System.Collections;
public class Oscillator : MonoBehaviour
{
public OscillatorType oscillatorType = OscillatorType.Position;
public float period = 1.0f;
public float amplitude = 0.01f;
public Vector3 direction = Vector3.one;
public bool initRandomPeriod = false;
private float startRandomPeriod = 0.0f;
private Transform myTransform;
private Vector3 origin;
void Start()
{
myTransform = transform;
if (initRandomPeriod)
startRandomPeriod = Random.Range(0.0f, Mathf.PI);
switch (oscillatorType)
{
case OscillatorType.Position:
origin = myTransform.position;
break;
case OscillatorType.Scale:
origin = myTransform.localScale;
break;
case OscillatorType.Rotation:
origin = myTransform.eulerAngles;
break;
}
}
void Update()
{
switch(oscillatorType)
{
case OscillatorType.Position:
myTransform.position = origin + direction * Mathf.Sin((startRandomPeriod + Time.time) * period) * amplitude;
break;
case OscillatorType.Scale:
myTransform.localScale = origin + direction * Mathf.Sin((startRandomPeriod + Time.time) * period) * amplitude;
break;
case OscillatorType.Rotation:
myTransform.eulerAngles = origin + direction * Mathf.Sin((startRandomPeriod + Time.time) * period) * amplitude;
break;
}
}
}
public enum OscillatorType
{
Position,
Rotation,
Scale
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment