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 SherlockHolmes · Oct 09, 2014 at 05:17 AM · javascriptmovement

Can't get speed of a rigidbody.

Hello!

My first script in Unity. I have a block that I can move, alright. Now I thought I'd fetch the speed of the rigidbody I have on that object, and if the speed is lower than 3, make it 10. Then it would obviously speed up quickly and then speed down again.

The moving part works. Everything works fine, but when I do this:

 #pragma strict
 
     var moveSpeed : float = 3; //speed of the player
     var maxSpeed : float = 3;
     var currentSpeed = rigidbody.velocity.magnitude;
 
     //called at start
     function Start () {  
 
 }
      //called every frame
     function Update () {
         //Fetch the input from the player, and convert
         //it into realtime movement
         
         if(currentSpeed < maxSpeed){
             moveSpeed = 10;
         
         }
         
         //translates the input into movement
         var translationZ : float = Input.GetAxis ("Horizontal") * moveSpeed; 
         var translationX : float = Input.GetAxis ("Vertical") * moveSpeed;
         //We don't want to let the player speed be dependent
         //on the frames per second, so let's fix the speed to
         //Time.deltaTime. By multiplying it, we basically
         //make it frame-independent. (At least, that's the plan).
         translationZ *= Time.deltaTime;
         translationX *= Time.deltaTime;
             
         //move desired creature along Z+X axis
         transform.Translate (translationX, 0, translationZ);
     
 }


This is the code. But as soon as I type the

 if(currentSpeed < maxSpeed){
         moveSpeed = 10;
         }

line, it gives me this:

UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration.

get_rigidbody can only be called from the main thread.

Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. PlayerMovement..ctor () (at Assets/Scripts/PlayerMovement.js:5)

Any help would be extremely appreciated. Thank you.

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
Best Answer

Answer by Baste · Oct 09, 2014 at 01:36 PM

You cannot assign the currentSpeed variable like that. Even if it was possible, it would be constant from the moment the script started, and your code wouldn't make sense. You need to move the assignment to currentSpeed inside the Update method:

 function Update () {
     //Fetch the input from the player, and convert
     //it into realtime movement
     currentSpeed = rigidbody.velocity.magnitude;
     if(currentSpeed < maxSpeed){
         moveSpeed = 10;
     }
     ...

There is still a big problem with your code! When you move a transform by setting it's position, that doesn't effect the physics engine in the way you would expect. In particular, the rigidbody does not register transform movement as movement. In essence, moving the transform directly means that the rigidbody's velocity is always zero.

To check speed by getting the rigidbody's velocity, you either have to move the rigidbody by setting it's velocity (making checking it redundant), or by adding force to the rigidbody by using AddForce. Hope that helps!

Comment
Add comment · Show 2 · 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 Bunny83 · Oct 09, 2014 at 01:40 PM 0
Share

perfect summary

+1

avatar image SherlockHolmes · Oct 09, 2014 at 06:57 PM 0
Share

That's a great answer. Thank you.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate character to the moving direction problems? 2 Answers

Movement using rigidbody.velocity to apply a constant force until stop 1 Answer

Why doesn't this motion script work? 2 Answers

Make Lerp or other more fluid or continuous 3 Answers

Networking Synchronize Problem. 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