Hello all, I bought this book Beginning 3D Development with Unity and I have a quick question. Its been a while since ive gotten into trig so here it goes. Here is the code snippet.
This is part of the platforming example in the book.
My question is why is the COS function used here and what role does it play. Previously i know trig was important in rotation i.e. u need to know the angle in something but here is it used to find the next point in 2d space before changing direction? Also why is 1 subtracted from here targetB.transform.position * (1-weight)? Many thanks. Loving the tool so far:)
This looks like a Slerp example. (Slerp uses Sin, not Cos)
OK, Cos is a circle starting at one and reaches zero at 90 degrees, (2 * Mathf.PI) == Mathf.Deg2Rad. So we are looking at a ramp of "speed * time) to get you a number between 1 and whatever Cos is at that time frame. Then * 0.5 then + 0.5. So your Cos is a number between 0 and 1, where the bottom of the Cos wave is at zero. (or 180 degrees is at zero)
The actual transform.position part looks almost exactly what a Lerp is, however it ramps between 1 and zero then back to 1 like a cos wave.
The subtraction of 1-weight is inverse to weight (which is a number between 1 and zero) So its a remainder. If you are at 0.75, then you are 0.75 of the position of targetA and only 0.25 the position of targetB.
Can anyone explain this in even simpler terms? The way the book describes it is this.
In order to set up a platform to move between two other platforms is that you have to set up
two targets target A and target B. two targets are set up horizontally and the moving platform is set up
between them. the moving platform also moves horizontally.
It just seems strange that COS would be used for this.
Try to make it easy so even a newbie can understand it:)
Cos may be used for an ease in and out at either end of the movement instead of having one uniform speed the entire way through.The X would be time and the Y would be distance from the center.
Here is a cosine wave to illustrate what I’m talking about:
Again, Cos is part of a circle. it starts at the top and 180 degrees later is at the bottom, it stays longer at the top and bottom than at the sides. So it’s approach and departure are slower than the bulk of the journey.
They do this, so that a person can get on it in plenty of time.
If you used a linear approach, then your departure, journey and approach would all be the same speed. Because of this, you would also then have to build in a timer for the destination for people to get off.