• 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
1
Question by hugofrost · Feb 12, 2020 at 08:34 PM · movementvector3velocityforce

How to stop player velocity completely

I have made a script that makes the character move. The problem is that the character is sliding around. I want to set velocity to 0 anytime the right buttons arent pressed. What line of could could do that. Thanks for any guidance and explaining. Herses the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     private bool moving;
 
     [SerializeField]
     Vector3 v3Force;
 
     [SerializeField]
     KeyCode keyPositive;
 
     [SerializeField]
     KeyCode keyNegative;
 
     void FixedUpdate()
     {
         if (Input.GetKey(keyPositive) || Input.GetKey(keyNegative))
         {
             moving = true;
         }
         else { moving = false; }
 
         if (moving)
         {
             if (Input.GetKey(keyPositive))
                 GetComponent<Rigidbody>().velocity += v3Force;
 
             if (Input.GetKey(keyNegative))
                 GetComponent<Rigidbody>().velocity -= v3Force;
         }
         else 
         {
             
         }
     }
 }
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
2

Answer by JohnnySix · Feb 12, 2020 at 09:11 PM

  GetComponent<Rigidbody>().velocity = Vector3.zero;

Should do it, in your else bit.

I'd maybe assign the Rigidbody in the Start rather than doing a GetComponent reference each time too.

Comment
Add comment · Show 5 · 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 hugofrost · Feb 12, 2020 at 09:43 PM 0
Share

Thanks for your answer. I tried your solution and it partly worked, the problem is that the player dosent move forward and backwards anymore, only to the sides. What do you think i did wrong?

avatar image hugofrost hugofrost · Feb 12, 2020 at 09:55 PM 0
Share

Nevermind, I solved it thanks to your help.

avatar image JohnnySix hugofrost · Feb 12, 2020 at 10:10 PM 0
Share

Great stuff. :)

Show more comments
avatar image JPhilipp · Feb 13, 2020 at 11:51 AM 0
Share

And in case that doesn't stop one's body completely, also add .angularVelocity = Vector3.zero (e.g. a rolling ball might otherwise keep moving again). Plus, consider increasing the Drag value in the Rigidbody options for a more natural, smoother halting of movement (after doing so, AddForce() strengths may need to be increased).

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

199 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 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

Vector3 Issues - Not Able To Solve Constructor issue 1 Answer

Bug, while checking if rigidbody is moving 0 Answers

I do you control movements and apply other forces at the same time ? 0 Answers

How to keep velocity and add onto it from any rotation. 1 Answer

Tried unsuccesfully many tips, vectorial force will keep adding to my object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges