Unity velocity referene in C# (getting it to print a single value in the console)

So, how can I get a single number for my object’s velocity?

I get that this value ought to come from Vector3, but Vector3 doesn’t give just 1 value, it gives 3 values of floats.

So, is it possible to have it print a single float? I thought that Debug.log(Vector3[0]); would do the trick, but I just get an error if I do that. Help?

Provide the line of code wich gives your an error.
Debug.log(Vector3[0]); is not correct, cause Vector3 is a reserved type (struct) name, you should put your variable name inside the log.

Velocity isn’t a single number (a scalar), it’s a vector.

But I bet what you’re asking for is speed. Speed is the magnitude of the velocity vector. So if your velocity is called “v”, then v.magnitude would give you the speed.

I’m pretty sure it’s also possible to index into the x, y, and z components of a Vector3 as [0], [1], and [2]; it’s just in your attempt, you didn’t give it a Vector3, you instead just named the Vector3 type. You can only get values out of an instance, not out of a type. What you did was basically the equivalent of saying Debug.Log(integer);, instead of giving it some particular integer to log.

But I doubt you’ll ever have need to index into a Vector3 by number anyway… if you want the x component, just say v.x; if you want the y, use v.y, and so on. And if you want the magnitude, say v.magnitude. (Replace “v” with whatever your particular vector is called.)

Yep, it’s possible but is not the obvious, however. And you are totally right with your comment.

Hmm, I have it on another computer. Anyway, you’re right, I don’t need the Vector3 argument, I need velocity.magnitude argument.

Well, I tried “Debug.Log (GameObject.rigidbody.velocity.magnitude);”

But it also didn’t work. I got an error saying:

Assets/TestSpeed.cs(7,39): error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.rigidbody’

using UnityEngine;
using System.Collections;

public class TestSpeed : MonoBehaviour {
    void Update()
{
        Debug.Log (GameObject.rigidbody.velocity.magnitude);

}
}

Remove GameObject, use just:

Debug.Log(rigidbody.velocity.magnitude);

In of itself it works, but when I try to put a variable on it I get

0
UnityEngine.Debug:Log(Object)
TestSpeed:Update() (at Assets/TestSpeed.cs:12)

And that’s it

using UnityEngine;
using System.Collections;

public class TestSpeed : MonoBehaviour {
    private float currentspeed;
    void Start ()
    {
        currentspeed = GetComponent<Rigidbody>().velocity.magnitude;
    }
    void Update()
{
        Debug.Log(currentspeed);

}
}


However, it seems to work only if I put the currentspeed = GetComponent().velocity.magnitude; in void Update, which is weird, because I don’t need to redefine it all the time, I just want it to print every frame.

Nothing weird here:) Your Rigidbody doesn’t have any velocity on Start, but it could have it later, so you need to get updated value (f. e. continuously on Update).

Ahh…the currentspeed variable can’t update on start, it can only take the initial value and save it! I see now. It makes sense actually. Well, I managed to get an object change color based on speed now :slight_smile: !!

I assume that GetComponent().velocity.magnitude; is the current combined vector speed, taking the X,Y,Z values all together and making one value?

If so, can I take only the X value, or only the Y value, or only the Z value?

Yes, you can, but it depends on what are you trying to achieve.

Each this value represents velocity along the x, y and z axis respectively.

That doesn’t make sense, because my object is only moving at the X and Z direction, and I don’t see any 0 on the console!

0.5008562
UnityEngine.Debug:Log(Object)
TestSpeed:Update() (at Assets/TestSpeed.cs:13)
0.5008562
UnityEngine.Debug:Log(Object)
TestSpeed:Update() (at Assets/TestSpeed.cs:13)
0.5007561
UnityEngine.Debug:Log(Object)
TestSpeed:Update() (at Assets/TestSpeed.cs:13)

etc etc… so it must be a combined vector, I assume.

1 Like

Then it is really weird:) Could you share your project?

Yes, .magnitude is the combined length of all three components; .x, .y, and .z are the speed along the X, Y, and Z axis respectively.

Nothing odd here that I can see.

Thank you JoeStrout. Can I isolate the component of X, Y and Z? That is to say, can I print for example the speed only along the Y direction?

ANTARES_XXI, I can share my project, sure, but JoeStrout just confirmed it’s the combined speed, so it shouldn’t be weird. You still want me to share?

Oh, is it magnitude value or velocity.y value? Seems like I’ve misunderstood your log source and then wrote pointless comment.

If it was about magnitude then all is right, just like JoeStrout said.

First off thanks for all your replies. I still want to understand though, how do I extract the current velocity for the x, z, and y separately?

You’re welcome… but maybe you should reread the thread. We’ve already answered that here, and here, and also pointed you to the documentation (which contains all the answers you seek) here.

If saying it twice hasn’t helped, I don’t see how saying it a third time is likely to do so.

Debug.log(velocity.x);
Debug.log(velocity.y);
Debug.log(velocity.z);

You also need to read up on value versus reference types.

1 Like