- Home /
arc object's position using sin
How would I convert this into Javascript. Right now this work inside of 3ds Max as is.
the number 12 references to 12 frames a second. So the for loop goes frame by frame. the number 20 refers to how high on the y position the object will go at it's peak
for i = 0 to 12 do ( y = abs(sin(180.0*i/12))*20.0 )
In unity I'm not sure how to right it out since there is the time.deltaTime thing. For my case I want the arc to take course of the life of 2 seconds.
My attempt in javascript
private var counter = 0.0;
function Start () {
InvokeRepeating("IncreaseCounter", 1.0, 1.0); //adds X to 'counter' each second
}
function IncreaseCounter () {
counter += 1.0;
}
function Update () {
stepY = ( Mathf.Abs( Mathf.Sin(180.0*counter / Time.deltaTime)) * 5.0);
transform.position.y = stepY;
}
Answer by Owen-Reynolds · Jul 20, 2012 at 03:36 AM
o The math keys of off the angle going from 0 to 180. To get it to take 2 seconds, set the angle to: 180*timePassed/2
. Check: when timePassed
is 2, 180*2/2 = 180.
o Real trig uses radians, not degrees, so before you use sin you have to convert by multiplying the angle by Math.DegToRad
(which is really just 2PI/360.)
o To get the time that passed, grab the current time, and count from there:
var startTime : float;
// In Start:
startTime = Time.time; // Grab the time the shot started
// In Update:
var timePassed : float = Time.time - startTime;
The you can use timePassed
and the 2-second angle in radians in your sin function. Of course, lots of other ways to write the same thing.
$$anonymous$$akes complete sense I've got what you've mentioned mostly implemented. I was hoping you could look over it and help me out. I just don't quite get the arc to happen over the course of 2.0 seconds just right. I got rather lost when you talked about implementing the radians ins$$anonymous$$d of degrees. I was hoping you could take a look at what I have so far and help me figure out what I'm missing. Just copy and paste this snippet below and place it on a sphere or object. Thanks
pragma strict
private var stepY = 0.0;
var startTime : float;
var spawnTrgts : GameObject[];
function Start () {
Destroy(gameObject, 2.0);
startTime = Time.time; // Grab the time the shot started
}
function Update () {
var timePassed : float = Time.time - startTime;
stepY = ( $$anonymous$$athf.Abs( $$anonymous$$athf.Sin(180.0*timePassed / 2.0)) * 5.0);
transform.position.y = stepY ;//(2 * Time.deltaTime); }
Look up Radians (anywhere, Wikipedia) for some background, then maybe Unity $$anonymous$$athf.
The input to $$anonymous$$athf.sin
is NOT the angle in degrees. It's the angle in Radians. You have to multiply the angle in degrees (`180*time/2`) by $$anonymous$$athf.Deg2Rad
to convert.
Since a circle has 6.28 radians, using degrees by mistake makes it spin or vibrate about 60 times too fast.
So taking the above function where do I add in, and what do I add in, to make it properly work. I've got everything you said in there except for the radians part. I was unclear how to get that integrated into the function.
Thanks for all your help Owen.
$$anonymous$$athf.Deg2Rad
is just a single, unchanging number. You don't give it any inputs or anything. You just multiply degrees by it. Look it up in the Unity manual for an example.
Not trying to be obtuse, but I assume you're going to want to fiddle with this more, so need to have done some assembly yourself.
Alright, I'll check out and see what I can get. I've got the x and z positions working properly. The last part is figuring out the y "arching" position using sin. I'll be looking into the deg2Rad things over the next day or so and I'll let you know how it goes. So far the script does exactly what I want except for the arch. But like you mentioned it currently vibrates about 100 times to fast. I'll be sure to post here the final results once I get it working.