Finding cursor position?

Hi. If the player clicks and their cursor is to the right of an object, I want to add force to move the object right. If the cursor is on the left, I want to add force to move it to the left. I cam up with this script, but I get these errors.

The best overload for the method
is not compatible with the argument
list ‘(float)’.

Operator ‘>=’ cannot be used with a
left hand side of type
‘UnityEngine.Ray’ and a right hand
side of type ‘float’.

function Update () {
    if (Input.GetButtonDown ("Fire1")) {
        var ray :  Ray = Camera.main.ScreenPointToRay (Input.mousePosition.x);
        if (ray >= gameObject.transform.position.x){
        	rigidbody.AddForce (Vector3.right * 5);
        if (ray <= gameObject.transform.position.x){
        	rigidbody.AddForce (Vector3.right *-5);


What am I doing wrong? Thanks

The mouse position is different from the 3D space location of the game object. Mouse position is in pixel coordinates on the screen. The Input.mousePosition for Raycasting just tells it where to generate the ray from, you can’t compare the mouse’s on-screen x-coordinate to a world space x-coordinate for an object.

It’s late and I’m not up to fully writing out the code but you’d want to use RaycastHit.point value for your comparison.

To get the 3D position of your click, you need to collide your way with a plane a retrieve the impact. The other way, and I suggest you choose that one, is to work in screen space.

var screenPos = Camera.main.WorldToScreenPoint( transform.position );
rigidbody.AddForce(Vector3.right * 
    (screenPos .x >= Input.mouseposition.x ? -5 : 5));

By the way, you can’t compare a ray with a float, nor cast a float as a Vector3 (those were your errors)