Can't get speed of a rigidbody.

Hello!

My first script in Unity. I have a block that I can move, alright. Now I thought I’d fetch the speed of the rigidbody I have on that object, and if the speed is lower than 3, make it 10. Then it would obviously speed up quickly and then speed down again.

The moving part works. Everything works fine, but when I do this:

#pragma strict

	var moveSpeed : float = 3; //speed of the player
	var maxSpeed : float = 3;
	var currentSpeed = rigidbody.velocity.magnitude;

	//called at start
	function Start () {  

}
 	//called every frame
	function Update () {
		//Fetch the input from the player, and convert
		//it into realtime movement
		
		if(currentSpeed < maxSpeed){
			moveSpeed = 10;
		
		}
		
		//translates the input into movement
		var translationZ : float = Input.GetAxis ("Horizontal") * moveSpeed; 
		var translationX : float = Input.GetAxis ("Vertical") * moveSpeed;
		//We don't want to let the player speed be dependent
		//on the frames per second, so let's fix the speed to
		//Time.deltaTime. By multiplying it, we basically
		//make it frame-independent. (At least, that's the plan).
		translationZ *= Time.deltaTime;
		translationX *= Time.deltaTime;
			
		//move desired creature along Z+X axis
		transform.Translate (translationX, 0, translationZ);
	
}

This is the code. But as soon as I type the

if(currentSpeed < maxSpeed){
        moveSpeed = 10;
        }

line, it gives me this:

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.

get_rigidbody can only be called from the main thread.

Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
PlayerMovement…ctor () (at Assets/Scripts/PlayerMovement.js:5)

Any help would be extremely appreciated. Thank you.

You cannot assign the currentSpeed variable like that. Even if it was possible, it would be constant from the moment the script started, and your code wouldn’t make sense. You need to move the assignment to currentSpeed inside the Update method:

function Update () {
    //Fetch the input from the player, and convert
    //it into realtime movement
    currentSpeed = rigidbody.velocity.magnitude;
    if(currentSpeed < maxSpeed){
        moveSpeed = 10;
    }
    ...

There is still a big problem with your code! When you move a transform by setting it’s position, that doesn’t effect the physics engine in the way you would expect. In particular, the rigidbody does not register transform movement as movement. In essence, moving the transform directly means that the rigidbody’s velocity is always zero.

To check speed by getting the rigidbody’s velocity, you either have to move the rigidbody by setting it’s velocity (making checking it redundant), or by adding force to the rigidbody by using AddForce. Hope that helps!