Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Blacklock · Nov 23, 2016 at 07:32 AM · c#physicsaddforceaxisgetaxis

How to stop a sphere that is using GetAxis from moving? (Roll-A-Ball Tutorial)

(I just wrote a huge wall of text for like 5 minutes and when I tried to post it, it all was just deleted, so I have to type everything out again, and now it's going to be shorter...)

I am on my second day of learning Unity and are currently doing the roll-a-ball tutorial. I am trying to add a restart button, that teleports the ball back to the start position. It all works good, but the ball will keep moving after I restart, but I want it to stay in a spot and not move by itself.

What I mean is that if I'm moving left at max speed and press 'R' to restart, then it will keep moving left fast for some time.

Here's my code that I just tried out and "hoped" would work, but I think that the problem I have here is that the GetAxis doesn't get restarted, and the next frame after the restart new AxisHorizontal/Vertical values will be assigned and so will AddForce:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     public float movementSpeed = 7f;
     private Rigidbody rigidSphere;
 
     void Start() {
         rigidSphere = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate() {
         float axisHorizontal = Input.GetAxis("Horizontal");
         float axisVertical = Input.GetAxis("Vertical");
 
         Vector3 movementForce = new Vector3(axisHorizontal, 0.0f, axisVertical);
         rigidSphere.AddForce(movementForce * movementSpeed);
 
         if (Input.GetKeyDown(KeyCode.R)) {
             transform.position = new Vector3(0, 0, 0);
             rigidSphere.AddForce(0, 0, 0);
             axisHorizontal = 0;
             axisVertical = 0;
         }
     }
 }

I tried googling on how to change the value that is taken from GetAxis, but others claim it's impossible to do so. So I have really no idea how to make it not keep moving forever...

And I also got two 'bonus' questions that have arised during those two days that I have been learning Unity, but they are not worth a new thread.

First, how do I make the ball stop moving after some time after I press a button? For example, I press 'W' just for a second, but the ball will keep moving forward forever and will never stop. What I want to achieve is after I for example press 'W' for a second, after a few seconds the ball will completely stop, it just seems that the value from GetAxis never goes back to 0. I tried to google about it, and I think it's something to do with Gravity in the Input Manager - I tried changing it, but no results.

And the second question is how I "pause time" in code, I googled and found out I have to use something like WaitForSeconds() and IEnumerator, but I couldn't do it, the docs didn't help neither.

I'd be really grateful if someone could help me with these problems.

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

0 Replies

· Add your reply
  • Sort: 

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

110 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 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 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 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 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 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 Airplane move forward faster INSTANTANEOUSLY? 1 Answer

Add force in the direction analog stick in pointing 0 Answers

Ball Speed is not increasing as per code 0 Answers

Script makes plane point in general direction but doesn't point fully... read description please! 1 Answer

Swipe Power Limit in a ball 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