• 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 NAYIR55 · Oct 22, 2014 at 03:44 AM · stopmomentum

Stop momentum of a ball

Hello, well like the title says, I'm making a simple game, and I want to stop the momentum of a ball.

When I make the input to move the ball simply goes forward, and when it respawns the momentum stays in a loop, like "Portal" game, I did a research of how to do it and didn't find anything useful

Here's my code (C#):

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour 
 {
 
     public float speed;
     private Vector3 spawn;
 
 
     void Start()
     {
         spawn = transform.position; 
     }
 
     void FixedUpdate()
     {
         float moveHor = Input.GetAxis ("Horizontal");
         float moveVert = Input.GetAxis ("Vertical");
 
         Vector3 move = new Vector3 (moveHor,0.0f, moveVert);
 
         rigidbody.AddForce (move * speed * Time.deltaTime);
 
         if(transform.position.y <= -4)
         {
             transform.position = spawn;
         }
     }

}

I did try the code

rigidbody.velocity = Vector3.zero; rigidbody.AddForce = Vector3.zero; rigidbody.angularDrag = Vector3.zero;

I did try making more variables, and same results

It didn't work

Thanks for reading

Comment
Add comment · Show 5
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 robertbu · Oct 22, 2014 at 03:45 AM 0
Share

Without seeing your code, it is difficult to see where you went wrong. rigidbody.velocity = Vector3.zero applied in the right place(s), should do the job. You can also set is$$anonymous$$inematic to true to stop something.

avatar image NAYIR55 · Oct 22, 2014 at 04:36 AM 0
Share

The code is not showing??

using UnityEngine; using System.Collections; public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { public float speed; private Vector3 spawn; void Start() { spawn = transform.position; } void FixedUpdate() { float moveHor = Input.GetAxis ("Horizontal"); float moveVert = Input.GetAxis ("Vertical"); Vector3 move = new Vector3 (moveHor,0.0f, moveVert); rigidbody.AddForce (move speed Time.deltaTime); if(transform.position.y <= -4) { transform.position = spawn; } } }

avatar image NAYIR55 · Oct 22, 2014 at 04:36 AM 0
Share
 using UnityEngine;
 using System.Collections;
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
 public float speed;
 private Vector3 spawn;
 void Start()
 {
 spawn = transform.position;
 }
 void FixedUpdate()
 {
 float moveHor = Input.GetAxis ("Horizontal");
 float moveVert = Input.GetAxis ("Vertical");
 Vector3 move = new Vector3 (moveHor,0.0f, moveVert);
 rigidbody.AddForce (move * speed * Time.deltaTime);
 if(transform.position.y <= -4)
 {
 transform.position = spawn;
 }
 }
 }
avatar image robertbu · Oct 22, 2014 at 04:57 AM 0
Share

I wasn't clear. The script you posted is fine, but no where in the script do you use rigidbody.velocity = Vector3.zero. So without seeing the context where you used this line, I cannot say why it did not work for you. And even seeing the code in context may not be enough information for an answer.

avatar image NAYIR55 · Oct 22, 2014 at 05:47 AM 0
Share

Well I don't know exactly where to use the line of code, I used it in here:

      if(transform.position.y <= -4)
     {
     transform.position = spawn;
     rigidbody.velocity = Vector3.zero;
     }

Is it ok? where can I use that?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by _Yash_ · Oct 22, 2014 at 07:32 AM

yes doing

 rigidbody.velocity = Vector3.zero;
 rigidbody.angularVelocity = Vector3.zero;

can fix it BUT here your input is adding force so make sure there is no input when it resets. You can also try disabling userInput for a couple of seconds after reset to see if it actually resets.

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 NAYIR55 · Oct 22, 2014 at 07:34 AM 0
Share

I did try those too, but where do I put those lines??

avatar image Dimling · Oct 22, 2014 at 07:56 AM 1
Share

I tested your code in Unity right now, and for me it works with this FixedUpdate() $$anonymous$$ethod that other people (for instance Yash) has suggested.

 void FixedUpdate()
     {
         float moveHor = Input.GetAxis ("Horizontal");
         float moveVert = Input.GetAxis ("Vertical");
         
         Vector3 move = new Vector3 (moveHor,0.0f, moveVert);
         
         rigidbody.AddForce (move * speed * Time.deltaTime);
         
         if(transform.position.y <= -4)
         {
             transform.position = spawn;
             rigidbody.velocity = Vector3.zero;
             rigidbody.angularVelocity = Vector3.zero;
         }
 
     }

If this is not working for you, do you have any other code in your project that might affect the current code?

Btw: you have to let go of the key when the ball is falling, if you keep pressing the key the ball, with this code, will of course be given force all the time and keep rolling.

avatar image NAYIR55 · Oct 22, 2014 at 08:10 AM 0
Share

After some time I could write those lines, and yes, I added some code after I posted this thread, thanks for the help

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Scripts stop working in Maximize On View and testing on Android 0 Answers

OnTriggerStay 2 Answers

How can an enemy deplete players health? 0 Answers

Make the first person controller transparent? 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