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 marcelobr · Oct 23, 2013 at 08:56 PM · speedsmoothcontrol

Control speed an rigidbody

I'm using this simple script when I hold the right to the speed increases smoothly but I do not know, do the speed back to zero smoothly see my script:

 public float acelerate = 5.0f;
     public float speedMin = 10.0f;
     public float speedMax = 30.0f;
     private float moveZ;
     public float speed = 0.0F;
     private float V;
     private float H;
     public float timePassed;
     
     
     void FixedUpdate () {
          V = Input.GetAxis("Vertical") *Time.deltaTime;
          H = Input.GetAxis("Horizontal")*Time.deltaTime;
     
          Vector3 vt = new Vector3(-V,0, H).normalized;
     
          rigidbody.AddForce( 0, 0,  vt.z*speed*8*Time.deltaTime);
     
     
       if(H>0){
         timePassed = Time.time;
     
             speed = Mathf.Lerp(speedMin, speedMax, timePassed/acelerate);
      }else{
         // my problem....... help me back for zero smoothly?
     }
Comment
Add comment · Show 2
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 darthbator · Oct 23, 2013 at 10:54 PM 0
Share

Why do you not just reverse your logic for lerping your force "forward"?

avatar image marcelobr · Oct 24, 2013 at 01:51 AM 0
Share

how so? explain better about that?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Oct 24, 2013 at 02:22 AM

You are mixing and matching things here. Think about a rocket in space. If you keep adding force (since there is no drag), the rocket will go faster and faster. Your code, and specifically your calculation of speed is does not work as any sort of real world calculation. For example using Time.time in the calculation assures that the longer the game runs, the more force you will add per frame. In addition, it seems strange to use horizontal twice...once in calculating vt, and once again to make speed calculations. My suggestion.

  • Delete lines 20 - 26 and 'speed' altogether.

  • Create a forceFactor variable and initialize it to a non-zero value. Start with 5.0;

  • In the inspector, select the object this script is attached to and increase the 'Drag' setting in the Rigidbody component (it is zero by default).

Your AddForce() will look something like:

 rigidbody.AddForce(0,0, H*forceFactor);

Since you are executing this in FixedUpdate(), you don't need deltaTime or fixedDeltaTime. Your code will all be about balancing your forward force and your drag. The drag will slow your ship down when you back off of 'Horizontal'. Play with some values until you get the ship to behave like you want.

Comment
Add comment · Show 6 · 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 marcelobr · Oct 24, 2013 at 04:12 AM 0
Share

good let tell you, i did this way why i don't your $$anonymous$$d!!!

  void control_speed(){
         R1 = Input.Get$$anonymous$$ey($$anonymous$$eyCode.JoystickButton7);
         
         
         // run
         // obs: increase speed if you are not running
         if(R1 && speed <100){
             
             speed +=50.0f*Time.deltaTime *100* Time.deltaTime;

          // stop run
         //decrease speed if are not run
             
         }if(speed >40 && !R1 && H >0
         || speed >40 && !R1 && H <0
         || speed >40 && !R1 && -V >0
         || speed >40 && !R1 && -V <0){
             
             speed -=50.0f *Time.deltaTime*100* Time.deltaTime;
         }
         
         
         // walk
         // obs: increase speed if walking
         if(speed <40 && !R1 && H > 0
         || speed <40 && !R1 && H < 0
         || speed <40 && !R1 && -V < 0
         || speed <40 && !R1 && -V > 0){
             
             speed +=50.0f*Time.deltaTime *100* Time.deltaTime;
         }
         // stop walk
         // obs: decrease speed if not walking
         if(speed >0.05 && !R1 && H ==0 && V ==0){
             
                    speed -=50.0f *Time.deltaTime*100* Time.deltaTime;
                 
         }
     }
avatar image robertbu · Oct 24, 2013 at 04:20 AM 0
Share

good let tell you, i did this way why i don't your $$anonymous$$d!!!

I don't understand what this means.

Given your logic here, there are a couple of things for you to consider. First, if you want to use explicit speeds with a Rigidbody, then you want to directly assign Rigidbody.velocity, not use AddForce(). Second, for checking speed with a rigidbody, you can use Rigidbody.velocity.magnitude. Or since all of your speed is in the 'Z' direction, you can check Rigidbody.velocity.z. It seems really convoluted to me to calculate some arbitrary 'speed' variable.

FYI: If you just want a fix for your current logic and want to slow down, a simple way is to multiply the velocity by some value less than 0 in FixedUpdate(). Put the following line in your else clause where you have "my problem....... help me back for zero smoothly?"

     transform.velocity *= 0.98; 

Adjust the 0.98 as appropriate for your app. This does not mimic real physics, but it looks good for many applications.

avatar image marcelobr · Oct 24, 2013 at 04:54 AM 0
Share

the logic i posted serve to when i hold the button in control to right, then the value var "speed" increases, and when i drop it then o value "speed" it will decrease.

its just it i want do increase the speed of the player smoothly and when i drop it the button the value will decrease you understand me?

avatar image robertbu · Oct 24, 2013 at 06:54 PM 0
Share

Here is a simple script that creates a linear increase in speed. Turn off gravity, and set Drag to 1.0 in the Rigidbody component.

 using UnityEngine;
 using System.Collections;
 
 public class Bug25c : $$anonymous$$onoBehaviour {
 
     private float speedIncreasePerFrame = 0.1f;
       
     void FixedUpdate() {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)) {
             float speed = rigidbody.velocity.magnitude;
             
             rigidbody.velocity = transform.forward * (speed + speedIncreasePerFrame);
         }
     }
 }

The 'Drag' setting in the Inspector controls how quickly it slows down. The 'speedIncreasePerFrame' controls how quickly it speeds up. But note this is a linear increase. You may want to use a percentage ins$$anonymous$$d.

Note you could do this with AddForce() ins$$anonymous$$d. Add a bit of force each frame to speed up. Let 'Drag' slow it down.

avatar image marcelobr · Oct 25, 2013 at 03:41 AM 0
Share

tell me what diferrence rigidbody.velocity and rigidbody.addForce? i think you dont understand me i want control o accelerate the variable "speed" to increase and decrease

Show more comments
avatar image
0

Answer by marcelobr · Oct 25, 2013 at 08:53 PM

ok thank you very much

Comment
Add comment · 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

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

15 People are following this question.

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Help me with smooth zoom 1 Answer

Time Freezer 1 Answer

Smooth Camera 1 Answer

Animation speed control - no smooth animation on slow down 3 Answers


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