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 dendens2 · Jan 22, 2013 at 02:55 AM · javascriptvelocity

How to get the velocity of an object

Hi, how do I get the velocity of an item? I tried this but it gives me an error.

 stepCounter = mouseLookScript.GetComponent(MouseLookScript).transform.parent.position.velocity * gunBobSpeed;
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
5

Answer by robertbu · Jan 22, 2013 at 03:09 AM

Velocity is part of the Rigidbody component. If you don't have a Rigidbody component and are instead moving the object yourself through the transform, then you will need to calculate the velocity by hand. If you do have a Rigidbody component, then you can do something like this in your script (C#):

 Rigidbody rb = GetComponent<Rigidbody>();
 Vector3 v3Velocity = rb.velocity; 

Velocity is a Vector3 giving back the velocity in the x, y, and z directions.

Comment
Add comment · Show 7 · 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 dendens2 · Jan 22, 2013 at 03:21 AM 0
Share

Oh, never$$anonymous$$d then I need speed. Do you know how to get that, and in JavaScript/Unity

avatar image robertbu · Jan 22, 2013 at 06:28 AM 0
Share

There is no speed that I'm aware of in Unity, only Velocity and only for game objects that have Rigidbody components. If you post a bit a code and described the problem you are trying to solve, someone here can give you some suggestions on how it might be accomplished.

avatar image AlucardJay · Jan 22, 2013 at 06:31 AM 1
Share
 rigidbody.velocity.magnitude

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html

http://docs.unity3d.com/Documentation/ScriptReference/Vector3-magnitude.html

avatar image dendens2 · Jan 22, 2013 at 09:41 PM 0
Share

I am trying to make a my gun bob as the character walks. I watched a tutorial on it but did things slightly different to fit my project. In his tutorial the stepcounter is

 stepcounter += Vector3.Distance(initialDistance, currentLocation)
or something like that. initial is the transform position when you start the game, while current is, well your current position. The problem with this is that the farther you walk away, the faster you would bob. Just setting it to = would work, but if you would go in certain directions it would go slower or faster. Do you guys understand?

I am using this to calculate the actual amount to bob:

currentGunBobX = $$anonymous$$athf.SmoothDamp(currentGunBobX, $$anonymous$$athf.Sin(stepCounter) * gunBobAmountX, gunBobXVelocity, stopBobTime);
        currentGunBobY = $$anonymous$$athf.SmoothDamp(currentGunBobY, $$anonymous$$athf.Cos(stepCounter * 2) * -1 * gunBobAmountY, gunBobYVelocity, stopBobTime);

I add the current gun bob to the position of the gun. Sorry if that did not make much sense I am in a rush right now, lol.

avatar image AlucardJay · Jan 23, 2013 at 02:42 AM 0
Share

Vector3.Distance is another way of calculating the magnitude between 2 Vectors , eg these both do the same thing :

 Vector3.Distance( initialDistance, currentLocation );
 ( currentLocation - initialDistance ).magnitude;

(initialDistance should really be called initialPosition). The problem seems to be the way stepCounter is calculated. Just setting it to = would work, but if you would go in certain directions it would go slower or faster. Yes, the further away from initialDistance you are, the higher stepcounter would be. If you do not have a rigidbody component that you can access for the velocity, you need to store a reference to the lastPosition of the object at the end of the update, then in the next update compare that to the current position. eg

 stepcounter = Vector3.Distance( lastPosition, currentLocation );
 // OR
 stepcounter = ( currentLocation - lastPosition ).magnitude;
Show more comments

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

13 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

Related Questions

How to access Vuforia C# library from Javascript 2 Answers

Recommend good .Net book for unity? 0 Answers

A node in a childnode? 1 Answer

What should i learn for unity C# or UnityScript? 4 Answers

Why can't i set my integer to -1? 2 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