I have a sphere in my world. As soon as the mouse pointer enters the world, i want the sphere's transforms to be guided by the mouse movement.In other words, the sphere should move based on mouse movements. I was trying this out looking at various other mouse-related posts and ended up with the below code , which gives me errors
You probably want to have a look at: Screen.lockCursor which will hide and lock the mouse position.
You can then use Input.GetAxis to read the mouse X and Y input, like this:
var horizontalSpeed = 2.0;
var verticalSpeed = 2.0;
function Update ()
{
// Get the mouse delta. This is not in the range -1...1
var h = horizontalSpeed * Input.GetAxis ("Mouse X");
var v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Translate (h, 0, v);
}
This will give you some very basic movement along the X-Z plane which corresponds to your mouse movement.
If you want controls a little more like "Marble Madness" - i.e. where the movement of the mouse adds a force to the game object, you could replace "Update" with "FixedUpdate", and relplace the line:
transform.Translate (h, 0, v);
with:
rigidbody.AddForce(h, 0, v);
Or even better, to convert the H and V to a force relative to the camera's orientation:
Your code isn't using valid Javascript syntax. For variable declarations, replace "Vector3" with "var". The variables will still be typed as Vector3 because of type inference. You can explicitly write "var ObjPosAtStart : Vector3 = ...", but that's not necessary.
As a side note, it's a good idea to keep your names consistent with conventions, in order to have more readable code. i.e., use lowercase for variable names, and uppercase for classes and functions.