- Home /
In what var can I know the size of velocity
I want to know velocity of my object. I have rigidbody.velocity, but i cant save it to my var Speed I try int or float but nothing work. Can u help me?
velocity
is a technical Unity term for when you let Unity's automatic physics system move the object, and makes it act "real." Your code sample below says you just snap to the mouse, so you don't have a velocity. Using "velocity" is confusing us.
You can remember how much you moved this frame. You'd have to save your old position, subtract and use magnitude. But that number will jump around a lot. A "smooth" mouse can easily move 10 pixels, 6 pixels, 0 pixels, -1 pixels (their hand jiggled,) 14 pixels ... . You can take the average speed, but that's trickier.
So, might help to know what you need it for.
Answer by sath · Dec 31, 2013 at 08:56 AM
Maybe this can help you
http://answers.unity3d.com/questions/23992/current-speed-of-an-object.html
http://answers.unity3d.com/questions/323400/reading-dorce-on-rigidbody.html
http://answers.unity3d.com/questions/13256/get-current-speed-of-an-object.html
Answer by robertbu · Dec 31, 2013 at 08:54 AM
Velocity is a Vector3. So you can do:
var myVelocity = rigidbody.velocity;
or
var myVelocity : Vectgor3 = rigidbody.velocity;
If you are just interested in the speed of the object, you can use the magnitude of the velocity:
var speed : float = rigidbody.velocity.magnitude;
This is my code I am moving with the platform. I dont have component rigidbody in the inspector but this code have also error Cannot convert 'float' to 'UnityEngine.Vector3'
private var ray : Ray;
private var rayCastHit : RaycastHit;
var Speed : float = rigidbody.velocity.magnitude;
function Update ()
{
if(Input.Get$$anonymous$$ouseButton(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, rayCastHit))
{
transform.position.x = rayCastHit.point.x;
}
}
rigidbody.velocity = Speed;
Debug.Log(Speed);
}
thank u
You need to save it as a Vector3. Either of the first two ways I outlined will work. Plus I don't know what you are trying to do with this code. If you initialize your velocity to rigidbody.velocity, you should do it in Awake() or Start(), but it makes no sense to save the velocity or the speed before any code has run that changes the velocity since velocity will be Vector3.zero. So you can do:
var myVelocity = Vector3(0,0,4);
and then just set the values in the Inspector if you like something different.
Then line 15 becomes:
rigidobdy.velocity = myVelocity;
Answer by TheShadowblast123 · Dec 31, 2013 at 09:16 AM
javascript right?
rigidbody.velocity is a Vector3 that as you know Stores the velocity
because of that you must put it in a Vector3. Here's a cool way I found out how to do it:
private var holdMyVelocity : Vector3;
holdMyVelocity = rigidbody.velocity;
"but I can't see it" go to the top corner of the inspector where there's a lock. just right of it there's a tiny drop down. click it and go to Debug. Now you can see loads of information you normally wouldn't be able to. The bigest benefit from this is that if you ever need to see your velocity again... you could always reactivate debug mode.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Calculating speed of rigidbody? 1 Answer
rigidbody.velocity.normalized application 1 Answer
Velocity for movement 0 Answers
Rotate Rigidbody Towards Velocity? [3d] 0 Answers