- Home /
How to use Mathf.SmoothDamp for audio ?
Hi ! First of all, I'd just like to clarify that I am quite new to Unity and not a programmer but a sound guy.
So here's the thing, I am currently working on a game where the main character is a robot that floats above the ground thanks to thrusters. When you move left or right, the pitch of his thrusters goes up according to speed (thanks to a script I borrowed from a friend). But the thing is, the pitch goes up too fast and I think SmoothDamp would be a good way to create some inertia to that pitchup/pitchdown action, right ?
Here's the script
var target : Transform;
var smoothTime = 0.3;
private var yVelocity = 0.0;
function Update () {
var newPosition : float = Mathf.SmoothDamp(transform.position.y, target.position.y,
yVelocity, smoothTime);
transform.position = Vector3(transform.position.x, newPosition, transform.position.z);
}
Would someone be kind enough to explain what variables should be changed ? I don't know much about programming but I'm guessing the target is not Transform (since this is about position) but I don't know what to replace it with.
Any help would be much appreciated. :)
Answer by Swaggre · May 02, 2014 at 07:50 PM
Do not do it according to speed but when the thrust button is pressed, raise the pitch and when it is not pressed lower it.. it might work
or smoothing it:
var maxPitch : float;
var lowestPitch : float;
var pitch : float;
var Smooth : int;
function Update () {
if(Input.GetKey(KeyCode.UpArrow)){//for example
pitch = Mathf.Lerp(pitch, maxPitch, Time.deltaTime * Smooth);
}
else if(pitch > lowestPitch){
pitch -= 0.001;//for example
}
}
I hope this helps