Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pjcarey97 · Aug 05, 2012 at 06:09 AM · c#moveforcesmoothstop

How do I spread a movement over several frames instead of one?

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:

     CharacterController cont;
     Vector3 force;
  
     void Start()
     {
         cont = GetComponent<CharacterController> ();
         force = (new Vector3(0,0,1f) * Time.deltaTime); 
     }
   
      void Update ()
      {
         if (Input.GetButtonDown ("Boost"))
         { 
             while(force.z > 0)
         {
             cont.Move (force);
             force.z -= (0.1f) * Time.deltaTime;
         }
      } 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Muuskii · Aug 05, 2012 at 06:16 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pjcarey97 · Aug 05, 2012 at 06:35 AM 0
Share

Thank you very much! This helped me out a lot, really!

avatar image pjcarey97 · Aug 05, 2012 at 06:37 AM 0
Share

One quick question, what if I wanted to do a full boost but with just one keypress?

avatar image ScroodgeM · Aug 05, 2012 at 10:57 AM 0
Share

for this task just declare a variable type boolean, 'boostEnabled' for example. then in Update() method make two separated things (fastwrited C#):

if (Input.Get$$anonymous$$eyDown("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.$$anonymous$$ove (force);
    force.z -= (0.1f) * Time.deltaTime;
}

...second block will work while boostEnabled is true even if button is unpressed.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How come my boost comes to an abrupt stop? 1 Answer

Map level selection slide smoothly 3 Answers

Smooth Move script for my spaceship 0 Answers

PlayerController Move() smoothly 0 Answers

Applying force to my object has no effect. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges