- Home /
Creating the After Effects Wiggle expression to effect rotation in C#
Hello, I'm pretty new to dealing with rotation in unity and programming in general. I've been trying to create a script that will rotate an object on the Z axis a random amount Left and then Right alternately for the duration of run time, and to have the rotation affected by 3 conditions, speed, amplitude, and frequency. I have no idea how to accomplish this as rotation does not behave the same way as position. The desired outcome can be seen here in a YouTube tutorial showing this effect created in After Effects. This is the very begging of the code i tried to produce to start creating something close the the desired results but instead ended up as pile of spaghetti code:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class WiggleV3 : MonoBehaviour { public int speed; public int upperLimit = 180; //frequency upper limit public int lowerLimit = -180; //frequency lower limit float current; // current z rotation float target; // target z rotation public bool Direction; //dictates wether its rotationg left or right Vector3 targetVector; //holds target Vector3 currentVector; //holds current // Use this for initialization void Start() { target = (Random.Range(upperLimit, lowerLimit)); //create random value Vector3 targetVector = new Vector3 (0, 0, target); // prepare to move there Vector3 currentVector = new Vector3 (transform.rotation.eulerAngles); //store the current rotation transform.Rotate(0, 0, target); //move there } // Update is called once per frame void Update() { if (Direction == true) //used to go right { Debug.Log("True"); Evaluate(); Move(); Direction = false; //set to switch to other IF } if (Direction == false) //used to go left { Debug.Log("False"); Evaluate(); Move(); Direction = true; //set to switch to other IF } } public void Evaluate() { target = (Random.Range(upperLimit, lowerLimit)); //find new random value Vector3 targetVector = new Vector3 (0, 0, target); //prepare it } public void Move() { transform.Rotate (targetVector, speed * Time.deltaTime); //move to said random value } }
NOTE: This does not currently work, nor are all the necessary conditions included
Thank you in advance for attempting to help me, its greatly appreciated! -Matt
Answer by MattOpara · May 31, 2018 at 04:54 PM
After a little work i figured it out! I decided I was going about it all wrong, rather than have a random amount of rotation checked against moved amount + current, just check for time passed and have that be what creates the randomness of movement, with a varying amount of time controlling when the object changes direction the desired effect was achieved.
using System.Collections.Generic;
using UnityEngine;
public class WigggleV4 : MonoBehaviour {
public float speed = 400f;
public int lowTime = 20;
public int highTime = 150;
public float target;
public float added;
float current;
public bool Direction;
public bool occuredOnce;
// Use this for initialization
void Start()
{
occuredOnce = false;
Debug.Log ("Start" + occuredOnce);
added = (Random.Range (highTime, lowTime));
target = Time.frameCount + added;
}
void Update ()
{
//Debug.Log (current);
current = Time.frameCount;
if (Direction == true)
{ //used to go right
Debug.Log ("left");
transform.Rotate (Vector3.forward * speed * Time.deltaTime);
if (occuredOnce == false)
{
Debug.Log ("Once False DT");
added = (Random.Range (highTime, lowTime));
target = Time.frameCount + added;
occuredOnce = true;
}
if (target == Time.frameCount)
{
Debug.Log ("target = frames DT");
Direction = false;
occuredOnce = false;
}
}
if (Direction == false)
{ //used to go left
Debug.Log ("Right");
transform.Rotate (Vector3.back * speed * Time.deltaTime);
if (occuredOnce == false)
{
Debug.Log ("Once False DF");
added = (Random.Range (highTime, lowTime));
target = Time.frameCount + added;
occuredOnce = true;
}
if (target == Time.frameCount)
{
Debug.Log ("target = frames DF");
Direction = true;
occuredOnce = false;
}
}
}
}
This script also allows for more control than the After Effects Wiggle Expression because you can chose not only the maximum amount of time to pass, but also a minimum amount as well! I hope this is helpful to someone, if you have suggestions on how to make this better, please let me know. -Matt