How to get/print the velocity of an object in X,Y,Z by using rigidbody.velocity?

Hi all,

I simply want to obtain the velocity of a cube in Unity 3D in x, y and z directions specifically. I am aware of GetComponent().velocity.magnitude method.

However, the magnitude is the combined length of all three components. What I want is the velocity along the X, Y, and Z axis respectively. I tied to use .x to read the velocity along the X direction but it didn’t work well. And the main issue is when I move the cube along X+, I get both positive and negative figures such as 1.4 and -0.7. It is not right. Can any of you help me out? Thanks a million.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RosSharp.RosBridgeClient
{
    public class JoyProvider : MessageProvider
    {
        public GameObject ObjectPosition;
        public Rigidbody Rb;

        // Use this for initialization
        private SensorJoy message;
        public override Type MessageType { get { return (typeof(SensorJoy)); } }
        public string FrameId;
        private JoyAxisReader[] JoyAxisReaders;// Declaring Arrays of JoyAxis
        private JoyButtonReader[] JoyButtonReaders;// Declaring Arrays JoyButton
        private void Start()
        {
            InitializeGameObject();
            InitializeMessage();  
        }
        private void Update()
        {
            if (IsMessageRequested)
                UpdateMessage();
        }
        private void UpdateMessage()
        {

            Rigidbody Rb = ObjectPosition.GetComponent<Rigidbody>();
            Vector3 vel = Rb.velocity;
            UnityEngine.Debug.Log(vel.x);
            RaiseMessageRelease(new MessageEventArgs(message));
      }
   }
}

Rigidbody.velocity gives you exactly what you want. I don’t really understand your problem tho. If the object is moving so it’s X position coordinate is increasing, then it’s velocity.x will be positive. If objects X position is decreasing, then it’s velocity.x will be negative