# GetComponent ().velocity returns zero

i have a cube whose movement is controlled using arrow keys. the following image shows the componnents attached to it:

and the contents of the scripts attached to the cube is as follows:

``````using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
public float forwardSpeedMult = 5f;
public float lateralSpeedMult = 10f;
public Vector2          minXY; // minimum XY the cube can move to
public Vector2          maxXY; // maximum xy the cube can move to
public float tilt=10f;

// Use this for initialization
void Start () {
}

void FixedUpdate () {
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
transform.position += new Vector3 (h * lateralSpeedMult * Time.deltaTime
, v * lateralSpeedMult * Time.deltaTime
, forwardSpeedMult * Time.deltaTime);
Vector3 tPosition = transform.position;
// Limit the X & Y to minimum and maximum values
tPosition.x = Mathf.Clamp(tPosition.x, minXY.x, maxXY.x);
tPosition.y = Mathf.Clamp(tPosition.y, minXY.y, maxXY.y);
transform.position = tPosition;
//tilting the player
print (GetComponent<Rigidbody> ().velocity);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0f, 0f, GetComponent<Rigidbody>().velocity.x*-tilt);
}
}
``````

when i move the cube, the velocity of the rigidbody retuned is either zero or something like 10^-7. the cube moves but its rigidbody velocity doesnt change. it could be seen in the console in the image.

Yes you guessed right - what you do is you just take object from one place and put it in another. If you want velocity to be there then you should assign it.

Something like:

``````private Rigidbody playerbody;
private void Awake()
{
playerbody = GetComponent<Rigidbody>();
}
//and then in code:
playerbody.velocity = YourVector3;
``````

P.S. The thing with storing reference in Awake or Start (depending on expected behavior) will save you some “horse power” and will run faster than Getting component each time. Just FYI.