- Home /
trying to move an object with the mouse
hey everyone,
i'm trying to move an object with a mouse and give it depth i've tried to add force and velocity and seem to be giving it either no movement or way way to much and shooting it off the screen.
i want to move the object towards the center of the screen when moving it around with the mouse, here is the code i have attempted so far but still having issues with.
var dragSpeed=1.0;
private var mag: float;;
public var isDragging : boolean=false;
static var dragging : boolean = false; // for camera use
private var obj : GameObject;
function Awake(){
obj = GameObject.Find("stone_point");
mag = Vector3.Magnitude(Vector3((obj.transform.position.x - Camera.main.transform.position.x), 0, (obj.transform.position.z - Camera.main.transform.position.z)));
}
function OnMouseDown(){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
rigidbody.useGravity = false;
rigidbody.freezeRotation = true;
rigidbody.AddForce((transform.position - ray.GetPoint(mag))*8.0);
//rigidbody.velocity=(transform.position - ray.GetPoint(mag));
dragging = true;
}
function OnMouseUp(){
rigidbody.useGravity = true;
rigidbody.freezeRotation = false;
dragging = false;
}
thanks for any and all help!
Answer by Berenger · Jan 30, 2012 at 09:56 PM
As the OnMouse functions are raised only when the mouse is hover the collider of the object the script is attached to, the vector (transform.position - ray.GetPoint(mag)) probably won't be what you want.
So cast a ray yourself at the input mouse up / down and use the impact on a plane to have the 3d position of your clip (that's not the only way). Or put a script on the plane to receive the OnMouse events.