- Home /
Trigonometric functions
I have a cage in my game hanging from the ceiling and I want it to dangle around. Therefor I wrote the following Script:
using UnityEngine; using System.Collections;
public class Cage : MonoBehaviour {
private float xrot;
private float zrot;
private float time;
void Start()
{
xrot = (Random.Range (0, 1000) / 1000.0f) * 10f - 5f;
zrot = (Random.Range (0, 1000) / 1000.0f) * 10f - 5f;
time = (Random.Range (0, 1000) / 1000.0f) * Mathf.PI;
}
void Update () {
if(Time.time - time >= 0){
transform.RotateAround (new Vector3 (transform.position.x, 12, transform.position.z), new Vector3 (1, 0, 0), xrot * Time.deltaTime * Mathf.Cos (Time.time - time));
transform.RotateAround (new Vector3 (transform.position.x, 12, transform.position.z), new Vector3 (0, 0, 1), zrot * Time.deltaTime * Mathf.Cos (Time.time - time));
}
}
}
The Code choses a random x and z rotation speed (positive or negative) and uses a cosine to rotate the cage. When the cage is instantiated it has a rotation of 270 and hangs straight down from the ceiling which is the reason why is has to have full speed at the beginning since it starts a the lowest and fastest part of the swing motion. The problem was all cages started swinging at the same time so I used "time" to start them at a different velocities. Now I do not know how to correct the start rotation of the cage. Having a random starting velocity demands the corresponding start rotation since those values are connected. I solved the poblem not very nice by letting them start to swing after the random delay but if you now look at the cage at the very beginning of the game they first do not move and then start abruptly which I do not want :D So is there any way how I can calculate the starting rotation for my cages with the value of "time"?
Thanks in advance! :D
Answer by NoseKills · Jun 07, 2015 at 07:55 PM
If all else fails, I think you could run a random amount of Updates for each cage when they are created to "fast forward" their position to a random spot.
Answer by Dave-Carlile · Jun 07, 2015 at 08:04 PM
How about using a Quaternion.Slerp? It takes from rotation
, to rotation
parameters, as well as t
which you change from 0 to 1 over time. When t
is 0 it returns from rotation
, and when t
is 1 it returns to rotation
, and when t
is in between it returns the appropriate value between from
and to
.
Your script would need to change t
over time, and when it reaches 1 you would reverse the direction and move it back toward 0, and then reverse and back toward 1, and so on.
Once you have that set up, all you need to do for a random start position is to start t
at a random value between 0 and 1. It would also be simple to have different swing speeds by varying how fast you modify t
.
Edit: You could also use a tweening library from the asset store for changing the value of t
, or mechanim, or it's fairly simple to handle yourself in Update
or a Coroutine or something.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotating Character 1 Answer
Orbit Camera Around Player And Add Force 3 Answers
Rotate towards movement direction 1 Answer