Answers for "Weapon Bullet Question - Steadily increasing sine wave"
http://answers.unity.com/questions/1379517/weapon-bullet-question-steadily-increasing-sine-wa.html
The latest answers for the question "Weapon Bullet Question - Steadily increasing sine wave"Answer by Major
http://answers.unity.com/answers/1379544/view.html
Of course it's possible!
A few things first though. To make things a little easier, I would use transform.Translate over Vector3.Lerp. This is because Vector3.Lerp isn't a consistent speed and as such would make it a little more difficult to sync things up.
Next, on line 27 you have Mathf.PI outside the sin function. If you were trying to convert to meters, it should look something like this instead:
Mathf.Sin (x * 2 * Mathf.PI)
This is because Mathf.Sin takes radians, and there are 2pi radians in a circle. So to convert to meters you have to cancel the radians by multiplying.
Okay, so here's some code:
public float amplitude = 2f;
public float amplitudePlus = 0.5f;
public float speed = 2;
public float cyclesPerSecond = 1;
float curTime = 0;
void Update () {
transform.Translate (transform.forward * speed * Time.deltaTime);
transform.position = new Vector3 (amplitude * Mathf.Sin (cyclesPerSecond * curTime * 2 * Mathf.PI), transform.position.y, transform.position.z);
amplitude += amplitudePlus * Time.deltaTime;
curTime += Time.deltaTime;
}
I'm gonna do my best to explain this, hopefully I succeed. We have 4 variables, amplitude, amplitudePlus, speed, and cyclesPerSecond. Amplitude will be the starting amplitude of our function, and we will add to it with amplitudePlus. Speed will be how fast the projectile is, and cyclesPerSecond will be how fast the projectile oscillates back and forth per second. The fifth variable, curTime, will keep track of our time to be used in our sin function.
The first line in Update () is pretty self explanatory. We are moving the projectile forward by our speed. Time.deltaTime is used here to make sure that we move in meters per second, and not meters per frame.
The next line is where the sin function is located. Inside Mathf.Sin there is the cyclesPerSecond times curTime times 2 times Mathf.PI. curTime is where on the sin wave we are. cyclesPerSecond scales the sin function on the x-axis to increase the cycles per period. Next it is multiplied by 2 times Mathf.PI to cancel the unit radians. So now, the period of the function is in seconds, and it will oscillate cyclesPerSecond time per second. This is because on the last line we are adding seconds to curTime:
curTime += Time.deltaTime;
Time.deltaTime being the time between each frame. Finally, the last thing that is done is the entire sin function is multiplied by the amplitude, scaling the function on the y-axis.
The second to last line is where amplitude is increased. Here I have it setup in terms of time as well. So the amplitude of the function will increase by amplitudePlus amount per second.
Time.deltaTime is important when looking at rates of change in Unity. Time.deltaTime is the approximate time between frames. You can use this value to ensure that you add values in terms of seconds, not frames.
Hope this was helpful, let me know if something needs clarified.Sat, 15 Jul 2017 20:01:53 GMTMajor