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 Fox-Handler · May 01, 2014 at 10:59 PM · horizontal movement

Rolling ball with Horizontal Input

Hi first time to the Unity forums. I was actually looking at a tutorial online and for the most part, I think I understand this code. Pretty much just rolling a ball around whenever I push a button. The only problem is that the ball keeps moving after I have let go of the button. It just keeps rolling and doesn't stop. Can anyone help me with this? Also could someone give a brief explanation of this code, since I'm somewhat only assuming how it works?

  #pragma strict
 
  var rotationSpeed : int = 100; // 100 units per second 
 
  function Update () 
  {
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime; 
     rigidbody.AddRelativeTorque (Vector3.back * rotation);
  }                                                          
                                                     
 
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 robertbu · May 01, 2014 at 11:12 PM

You are using a Rigidbody and the Physics engine to move the ball. In the absence of friction or drag, any force applied will last forever. Think shooting a rocket in space. After the engine quits, the rocket keeps going. In order to get the ball to slow down, you can do any of:

  1. Up the AngularDrag of the Rigidbody. It can be done in the Inspector, or it can be done in code.

  2. Direct manipulation of the AngularVelocity. This can be tricky.

  3. Add Physic materials to the colliders of the ball and to the surface the ball rolls on. These Physic materials define the amount of friction that occurs during collisions.

You can get a starter set of Physic materials:

 Assets > Import Package > Physic Materials

There are a few other ways as well like adding a counter force to the rotation, or you can set the 'isKinematic' flag to true for an immediate stop.


The code above is adding torque (rotation force) to the ball. It uses the input from the "Horizontal" axis which can be tied to many things. Usually in the editor it is arrow or aswd keys, but it can use a joystick or mouse. The amount of rotation is scaled by Time.deltaTime to keep it consistent across varying frame rates and devices.

Comment
Add comment · Show 1 · 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 Fox-Handler · May 01, 2014 at 11:49 PM 0
Share

thank you! great answer. this makes much more sense now

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

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Combining 2 Vectors for Diagonal Movement 1 Answer

How can I make my character jump & move him by zone 0 Answers

Rotation too complicated 3 Answers

how can i instantly change the the direction of a force and speed aplyd to a 2D object 1 Answer

How to drag and move a character left and right using touch controls in an endless runner game? 0 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