• 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 /
This question was closed Nov 07, 2016 at 07:03 PM by DLEnfield for the following reason:

The question is answered, right answer was accepted

avatar image
Question by DLEnfield · Nov 04, 2016 at 08:18 PM · c#scripting problemvelocityrigidbody.velocity

Velocity on a rigidbody won't stop?

So this is probably going to be a really simple fix to a really stupid problem but I've been trying to figure this out for hours to no avail.

I have a basic script for a mobile VR project in which the 'character' (in this case the main cam with a CC and Rigidbody attached) starts off stationary, then when Fire1/Mouse0 is clicked, it starts to fly in whatever direction the user is facing using velocity.

The issue is, when the user clicks the button again, the camera should stop completely in place, until clicked again and the camera starts moving, etc etc.

However, when the button is clicked to stop the camera moving, it just continues on it's path, regardless of where the user looks.

I've been experimenting for a good while and it definitely seems to be the velocity line;

cc.velocity = transform.forward * forwardSpeed;

that doesn't seem to want to stop when the button is triggered a second time. But I'm fairly new to all this so it may just be an oversight.

I'll attach the code I've used, if anyone needs more details I'm happy to provide. Thanks in advance.

 public Transform vrCamera;
 private Rigidbody cc;

 public float forwardSpeed = 50f;

 Vector3 axis;
 float rotationY;
 float rotationX;
 float rotationZ;

 bool start = false;




 void Start () {
     cc = GetComponent<Rigidbody> ();


     vrCamera = Camera.main.transform;
 }


 void Update () {

     if (Input.GetButtonDown ("Fire1")) {
         

         start = !start;
     }

     if(start)
     {

         cc.velocity = transform.forward * forwardSpeed;

     
 }
         

}

}

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by LK84 · Nov 04, 2016 at 08:27 PM

Well, you need to set the velocity back to zero, right? I don't see where you do that. cc.velocity remains at the last value before clicking. btw, I wouldn't set the velocity of a rigidbody directly.

Comment
Bunny83

People who like this

1 Show 0 · 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

Answer by iAmAwsum · Nov 04, 2016 at 08:37 PM

Have you tried changing

cc.velocity = transform.forward forwardSpeed;*

to

cc.velocity = Vector3.zero;

and adding

cc.angularVelocity = Vector3.zero;

or you could also try

cc.isKinematic = true

Read This

Comment

People who like this

0 Show 0 · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Rigidbody2d.velocity Inaccurate 1 Answer

Bullet not moveing forward,Bullet not moveing 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Rigidbody.velocity giving weird result 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