- Home /
need help with a boost effect
i want to make a boost effect when i press a desired key (in my case left alt)....The thing is I want to do it i nthe same script how would i do it based on these lines of code
if ((groundtrigger.triggered==0)&&(Input.GetButton("Fire1"))&&(speed<800)) speed+=Time.deltaTime*240;
if ((groundtrigger.triggered==0)&&(Input.GetButton("Fire2"))&&(speed>600)) speed-=Time.deltaTime*240;
if ((groundtrigger.triggered==0)&&(Input.GetButtonDown("Boost"))&&(speed<800)) transform.Translate(0,0,speed*Time.deltaTime*10);
please tell me if you require more code (for reference)
what i want to do is make the object boost and then it slows down smoothly
can you help me?
You could have a 'currentBoost' variable, which goes down by some multiple of Time.deltaTime every frame. When you want to activate the boost, press your key and set it to something high, and then multiply all your speed variables by the current boost. Also, use $$anonymous$$athf.Clamp to make sure the boost never goes below a certain number- make sure you don't have negative boost, making you go backwards!
could you maybe code it out slightly in an example ....because thats how i usually grasp a concept.....by seeing example code
Answer by Tseng · Oct 06, 2011 at 01:37 AM
Well, you need different variables, like currentSpeed
and speed
and interpolate it, and always only update speed
variable.
if ((groundtrigger.triggered==0)&&(Input.GetButton("Fire1"))&&(speed<800)) speed+=Time.deltaTime*240;
if ((groundtrigger.triggered==0)&&(Input.GetButton("Fire2"))&&(speed>600)) speed-=Time.deltaTime*240;
currentSpeed = Mathf.Lerp(currentSpeed, speed, Time.time * smoothSpeed),
if ((groundtrigger.triggered==0)&&(Input.GetButtonDown("Boost"))&&(speed<800)) transform.Translate(0,0,currentSpeed*Time.deltaTime*10);
This will smooth both, acceleration as well as deceleration...
Answer by rohan bhangui · Oct 06, 2011 at 11:39 AM
okay great......i found the solution.......
i added this line of code
if ((groundtrigger.triggered==0)&&(Input.GetButton("Boost"))&&(speed<800)) transform.Translate(0,0,speed/200);
this allows me too boost......all i have to wrry about is smoothing it out........thx all :)
This is no forum, only answers to the questions are posted there, everything else goes into the comments: Read the FAQ: http://answers.unity3d.com/page/faq.html