Rotate cube as if it were floating in the sea
Hello, dear community!
I'm trying to make a cube seem floating on a surface, I've found a script here: http://answers.unity3d.com/questions/822484/a.html
public Vector3 from = new Vector3(0f, 0f, 135f);
public Vector3 to = new Vector3(0f, 0f, 225f);
public float speed = 1.0f;
void Update() {
float t = Mathf.PingPong(Time.time * speed * 2.0f, 1.0f);
transform.eulerAngles = Vector3.Lerp (from, to, t);
}
It works great! But it only "lerps" between 2 vectors. Is it possible to lerp between 4 or 5 different cube rotations? The help is appreciated!
EDIT: I've tried writing the following script, but it doesn't work: "Cannot implicitly convert float to int" in the "transform.eulerAngles = Vector3.Lerp (fromarrays[randomnumber1from], toarrays[randomnumber2to], t);"
using UnityEngine;
using System.Collections;
public class rotation12 : MonoBehaviour {
public Vector3 from = new Vector3(0f, 0f, 135f);
public Vector3 to = new Vector3(0f, 0f, 225f);
public Vector3 from2 = new Vector3(0f, 0f, -100f);
public Vector3 to2 = new Vector3(0f, 0f, 100f);
public Vector3 from3 = new Vector3(0f, 0f, -200f);
public Vector3 to3 = new Vector3(0f, 0f, 200f);
public Vector3 from4 = new Vector3(0f, 0f, -300f);
public Vector3 to4 = new Vector3(0f, 0f, 300f);
public Vector3[] fromarrays;
public Vector3[] toarrays;
public float randomnumber1from;
public float randomnumber2to;
public float speed = 1.0f;
void Start(){
fromarrays[0]=from;
fromarrays[1]=from2;
fromarrays[2]=from3;
fromarrays[3]=from4;
toarrays [0] = to;
toarrays [1] = to2;
toarrays [2] = to3;
toarrays [3] = to4;
}
void Update() {
rotatefuncrion ();
}
void rotatefuncrion(){
float t = (Mathf.Sin (Time.time * speed * Mathf.PI * 2.0f) + 1.0f) / 2.0f;
randomnumber1from = Random.Range (0, 3);
randomnumber2to = Random.Range (0, 3);
transform.eulerAngles = Vector3.Lerp (fromarrays[randomnumber1from], toarrays[randomnumber2to], t);
}
}
Could use suggest what is the reason?
Your error is due to the fact that you declared randomnumber1from
, and randomnumber2to
, as float
. If you want to use them to index into an array, you must declare them as int
.
But, I would suggest a different approach: why not generate a random angle to rotate "to", rather than use arrays?
Vector3 to = new Vector3(0,0, Random.Range (0,360));
Hey! Thank you for a reply, I cannot find an Upvote button, new forums require some time to get used to a new style. EDIT: found an upvote button
I've rewritten the code, it works almost as I wanted, except it does it many times per second. Is there a way to stop Random.Range till previous Lerp is done?
Here is the code.
using UnityEngine;
using System.Collections;
public class rotation12 : $$anonymous$$onoBehaviour {
public Vector3 from = new Vector3(0f, 0f, 135f);
public Vector3 to = new Vector3(0f, 0f, 225f);
public Vector3 from2 = new Vector3(0f, 0f, -100f);
public Vector3 to2 = new Vector3(0f, 0f, 100f);
public Vector3 from3 = new Vector3(0f, 0f, -200f);
public Vector3 to3 = new Vector3(0f, 0f, 200f);
public Vector3 from4 = new Vector3(0f, 0f, -300f);
public Vector3 to4 = new Vector3(0f, 0f, 300f);
public Vector3[] fromarrays;
public Vector3[] toarrays;
public int randomnumber1from;
public int randomnumber2to;
public float speed = 1.0f;
void Start(){
fromarrays = new Vector3[4];
fromarrays[0]=from;
fromarrays[1]=from2;
fromarrays[2]=from3;
fromarrays[3]=from4;
toarrays = new Vector3[4];
toarrays [0] = to;
toarrays [1] = to2;
toarrays [2] = to3;
toarrays [3] = to4;
}
void Update() {
rotatefunction ();
}
void rotatefunction(){
float t = ($$anonymous$$athf.Sin (Time.time * speed * $$anonymous$$athf.PI * 2.0f) + 1.0f) / 2.0f;
randomnumber1from = Random.Range (0, 4);
randomnumber2to = Random.Range (0, 4);
transform.eulerAngles = Vector3.Lerp (fromarrays [randomnumber1from], toarrays [randomnumber2to], t);
}
}
Thank you very much! Is it possible to reward a user? I don't figured out all the possibilities of the new forums.
Those little up & down chevrons (Sargent stripes) that appear when you mouse over a comment can be used to upvote a comment. I'll create a new answer that combines both comments, then you can click "Accept" to mark it as "the answer".
Answer by Glurth · Sep 17, 2015 at 07:37 PM
Your error is due to the fact that you declared randomnumber1from
, and randomnumber2to
, as float
. If you want to use them to index into an array, you must declare them as int
.
"Is there a way to stop Random.Range till previous Lerp is done?" Well, the parameter you pass to Lerp should be a value between 0 and 1. When it reaches 1, the Lerp could be considered "done" as it has reached the "to" Vector. In this case, I would compare t>0.95 in-case of rounding/timing problems
Worth noting, that since you are passing a cyclical sine wave, you might consider it "done" after a full "cycle", rather than simply when the value reaches 1. In this case, you will need to remember what "t" value your "cycle" started on, and wait till "t" it gets close to that number again. (create & use a class member variable to store this value, so it remains "in-scope" across multiple calls to update().)
Your answer
Follow this Question
Related Questions
How to calculate and use new direction angle as a new forward direction. 1 Answer
I need help in rotating cube by 90 degree with swipe in all axis 0 Answers
Instantiate bullet Impact Effect on cube , rotation problem 1 Answer
Rotating a cube in 3D ,Rotating a cube into specific coordinates with given input 0 Answers
Unity Rotating a Cube 0 Answers