Gameobject moving along with mouse - help (video included)

Hey.
I am having trouble with scripting gameobject (cube).
I would like that cube would follow mouse cursor on X and Y axes.
I have done some scripting, everything works fine, except cube is following cursor at very slow speed.

  1. I would like that cube would be more responding on what i do with my mouse.
  2. I would like that cube would follow cursor at the same speed that cursor moves.

My script:

function Update () {

var halfW:float = Screen.width/2;
transform.position.x = (Input.mousePosition.x - halfW)/halfW;
var halfH:float = Screen.height/2;
transform.position.y = (Input.mousePosition.y - halfH)/halfH;
}

Second problem i have is:

  • when i hit Sphere(ball) with Cube with little more power, ball falls through Cube.

Video presentation of those two problems: http://screencast.com/t/qK1acA87F

If someone know how to solve those two, or at least one problem, pleas help !
I will own you one.

Anze

Ok…

  1. You cannot simply refer screenposition to world position so id propose that you take a look at
    Unity - Scripting API: Camera.ScreenToWorldPoint
    There you can pass the mousepos as a parameter and use the result for your cube. It should put it directly beneth your cursor.

  2. Move your cube in FixedUpdate, if that doesnt fix it, apply rigidbodies to your spheres and your cube and use rigidbody.MovePosition on your cube instead of assign the transform directly. Freeze the cubes rigidbody movement/rotation in the inspector.