Roll forward mouse

Hey,

I am creating a 2.5D marble roller game. I have made a script, but it is not working perfectly, the marble is not rolling, and it is hard to controll.

Here is the script:

var speed 			: float = 1.0;
var maxSpeed		: float = 3.0;

var position		: Vector3;

var cube 			: GameObject;
var curSpeed		: float = 0.0;

function Update () 
{
	if(Input.GetMouseButton(0))
	{
		var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		
		if (Physics.Raycast (ray, hit))
		{
			position.x = hit.point.x;
			position.y = 0;
			position.z = hit.point.z;
		}
		
		if (curSpeed < maxSpeed)
		rigidbody.AddTorque(position.x * speed * Time.deltaTime, 0, position.z * speed * Time.deltaTime);
	}
		
	curSpeed = Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z).magnitude;
}

The scene contains the following:
A scaled cube for the ground.
A sphere with a rigidbody, physics material, and the script.

Can you give me some advice, to improve it to be better?

Thanks!

I’m a newbie, but nevertheless can see something strange in your script:

You change an object’s velocity instead of it’s acceleration. From a physical point of view this is unrealistic.

I think the marble is not rolling because you have to apply a force, causing the marble to get speed. There must be some command in unity to apply a force to an object. You should use this instead of adjusting the velocity directly.