- Home /
Getting to move object with mouse position?
I have tried to make a card game where you can click and drag the cards around, but i can't seem to figure out how to move the object, with the mouse position without flinging miles away in the game scene. Can someone help me out, i am in 2nd of college (english) and know moderate jarva scripting.
He is the script i borrowed off someone else can someone modify it and get the cards to follow the mouse position.
#pragma strict
// Attach this script to an orthographic camera.
private var object : Transform; // The object we will move.
private var offSet : Vector3; // The object's position relative to the mouse position.
function Update ()
{
var ray = camera.ScreenPointToRay(Input.mousePosition); // Gets the mouse position in the form of a ray.
if (Input.GetButtonDown("Fire1"))
{ // If we click the mouse...
if (!object)
{ // And we are not currently moving an object...
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, Mathf.Infinity) && (hit.collider.tag == "BlueCard"))
{ // Then see if an object is beneath us using raycasting.
object = hit.transform; // If we hit an object then hold on to the object.
offSet = object.position-ray.origin; // This is so when you click on an object its center does not align with mouse position.
}
}
}
else if (Input.GetButtonUp("Fire1"))
{
object = null; // Let go of the object.
}
if (object)
{
object.position = Vector3(ray.origin.x+offSet.x, object.position.y, ray.origin.z+offSet.z); // Only move the object on a 2D plane.
}
}
Comment