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 letsfailsafe · Jun 22, 2013 at 11:17 AM · guivelocitydisplayvehicle

Displaying velocity of a game object

Hi, I am working on to get a game object's velocity. I got stock on displaying the values

 using UnityEngine;
 using System.Collections;
 
 public class UnitDisplayScript : MonoBehaviour {
 
         
     void FixedUpdated() 
     {
         speed = GameObject.rigidbody.velocity.magnitude;
     }
     
     void OnGUI()
     {
         GUI.Box(new Rect(10,10,100,90), "Measurements");
         
         GUI.Label(new Rect(20,40,80,20), speed + "m/s");
     }
 }

GUI does appear on the left top corner of the display. Just that velocity does not change...

If it does display will it be resultant velocity? Also, how would I display vertical and horizontal velocity?

Comment
Add comment · Show 3
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 ByteSheep · Jun 22, 2013 at 11:58 AM 2
Share

FixedUpdated() should be FixedUpdate()

avatar image Georgegohl888 · May 01, 2018 at 06:54 PM -1
Share

im trying to do the same but it will only display 0. any ideas?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class yeet : $$anonymous$$onoBehaviour {
 
     private float speed;
 
     void FixedUpdated()
     {
         speed = GetComponent<Rigidbody>().velocity.magnitude;
     }
     void OnGUI()
     {
         GUI.Box(new Rect(10, 10, 100, 90), "$$anonymous$$easurements");
 
         GUI.Label(new Rect(20, 40, 80, 20), "velocity is:" + speed );
     }
 }
 
avatar image ByteSheep Georgegohl888 · May 01, 2018 at 07:59 PM 0
Share

Please don't post questions as answers - it looks like your problem is that you copied the FixedUpdated typo... This should be changed to FixedUpdate else Unity won't call it and speed will remain zero.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Nanity · Jun 22, 2013 at 11:36 AM

GameObject is the class where all the objects inherit from, in order to access the specific gameObject, write it with a non-capital letter. Change

 GameObject.rigidbody.velocity.magnitude

to one of those alternatives

 gameObject.rigidbody.velocity.magnitude
 rigidbody.velocity.magnitude
 GetComponent<Rigidbody>().velocity.magnitude

Note that rigidbody and GetComponent< Rigidbody>() are identical, so you better cache the rigidbody parameter in the Start Function and use that one (_rigidbody) all the time:

 private Rigidbody _rigidbody;

 private void Start()
 {
     _rigidbody = rigidbody;
 }

This may be micro optimizing, but as you plan to call this every frame it's worth a thought.

Comment
Add comment · Show 4 · 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 letsfailsafe · Jun 23, 2013 at 03:28 AM -1
Share

I am getting an error: "The name 'speed' does not exist in the current context"

 using UnityEngine;
 using System.Collections;
 
 public class UnitDisplayScript : $$anonymous$$onoBehaviour {
 
     void FixedUpdated() 
     {
         speed = rigidbody.velocity.magnitude;
     }
     
     void OnGUI()
     {
         GUI.Box(new Rect(10,10,100,90), "$$anonymous$$easurements");
         
         GUI.Label(new Rect(20,40,80,20), speed + "m/s");
     }
 }

What have I done wrong?

avatar image Nanity · Jun 23, 2013 at 09:02 AM 0
Share

You have to declare the variable globally first and put it in line 5:

 private float speed;

And like merry_christmas correctly noted the function should be called FixedUpdate() and not FixedUpdated(), otherwise it's not going to be called.

avatar image AgentFire · Aug 12, 2014 at 01:29 PM 0
Share

You can't inherit the GameObject class.

avatar image Jr-FreeMaNn · Jan 29, 2018 at 04:28 PM 0
Share

Thanks man.You are the best

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

19 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

Related Questions

Displaying varying text 1 Answer

GUI Overlay Display 0 Answers

Why does the font selected for my GUI Skin not display correctly? 1 Answer

Displaying PlayerPref As Text Using New Unity GUI 1 Answer

How can I add a colored border to my text box? 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