|
I'm trying to propel my character forward with the spacebar. So I set a force as a vector3, and a charactercontroller, and when the spacebar is pressed, I enter a while(force is more than 0) loop. In the loop, I move forward by whatever force is equal to, and then subtract a little from force. The result is an abrupt, 1-frame motion that I'm looking to change to a several frame motion with a smooth stop. Can anybody give me a hint on how to do this? Here's that chunk of my code IF IT HELPS:
(comments are locked)
|
|
You have to realize that Update() is going to be called every frame unless the script is disabled. So ask yourself: Why do you need a while loop in a function that is called every frame? What is going to happen if you remove it altogether? Addendum: You should replace GetButtonDown with GetButton; The first one returns true the first frame that the button is pushed, the second returns true until the button is released. Thank you very much! This helped me out a lot, really!
Aug 05 '12 at 06:35 AM
pjcarey97
One quick question, what if I wanted to do a full boost but with just one keypress?
Aug 05 '12 at 06:37 AM
pjcarey97
for this task just declare a variable type boolean, 'boostEnabled' for example. then in Update() method make two separated things (fastwrited C#): if (Input.GetKeyDown("Boost") {boostEnabled = false;}...this will turn on boost on key press if (boostEnabled)
{
...check if boost can work - have a boost power etc, disable 'boostEnabled' if can't work and exit if block
cont.Move (force);
force.z -= (0.1f) * Time.deltaTime;
}...second block will work while boostEnabled is true even if button is unpressed.
Aug 05 '12 at 10:57 AM
ScroodgeM
(comments are locked)
|
