- Home /
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;
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.
Oh, never$$anonymous$$d then I need speed. Do you know how to get that, and in JavaScript/Unity
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.
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.
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;
Your answer
Follow this Question
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