Cloning objects infinitely - drag and drop

Hi all,

Just a quick run-down before I post the code, this is a 2D board game that revolves around allowing players to place as many board tokens on the board as they like, so just the gain a visual image there would be token types on the side of the game screen where players can drag and drop onto the board to create a copy of that token, they can create as many copies of any given token as many times as they like.

Right now I’m able to clone an object but no indefinitely (about once or twice) then the drag and drop function stops working for some odd reason (maybe something to do with the ray casting?), I’m pretty stumped on this one, hoping someone can help :).

Here’s the code:

var grabbed : Transform;
var grabDistance : float = 10.0f;
var grabLayerMask : int;
var grabOffset : Vector3; 
var useToggleDrag : boolean;
var origPos : Vector3;

//reference to the prefab we instantiate on drop
var dropObject : Transform;

function Update () 
{
    if (useToggleDrag)
    {
        UpdateToggleDrag();
    } 
    else 
    {
        UpdateHoldDrag();
    }
}

// Toggles drag with mouse click
function UpdateToggleDrag () 
{
    if (Input.GetMouseButtonDown(0))
    { 
        Grab();
    } 
    else 
    {
        if (grabbed) 
        {
            Drag();
        }
    }
}

// Drags when user holds down button
function UpdateHoldDrag () 
{
    if (Input.GetMouseButton(0)) 
    {
        if (grabbed)
        {
            Drag();
        } else { 
            Grab();
        }
    } 
    else 
    {
        if(grabbed)
        {
           //restore the original layermask
           grabbed.gameObject.layer = grabLayerMask;
        }
        grabbed = null;
    }
}

function Grab() 
{
    if (grabbed)
    { 
       grabbed = null;
    } 
    else 
    {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        
        if (Physics.Raycast(ray, hit))
        {          
            grabbed = hit.transform;
            
            if (grabbed.parent)
            {
                grabbed = grabbed.parent.transform;
            }
            
            //set the object to ignore raycasts
            grabLayerMask = grabbed.gameObject.layer;
            grabbed.gameObject.layer = 2;
            
            //now immediately do another raycast to calculate the offset
            if (Physics.Raycast(ray, hit))
            {
                grabOffset = grabbed.position - hit.point;
            } 
            else 
            {
                //important - clear the grab if there is nothing
                //behind the object to drag against
                grabbed = null;
            }
        }
    }
}

function Drag() 
{    
    var hit : RaycastHit;
    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
    if (Physics.Raycast(ray, hit))
    {      
		grabbed.position = hit.point + grabOffset;
    }
    
}


function OnMouseUp()
{
    //instantiate new object
	var newToken = Instantiate( dropObject, origPos, transform.rotation );
}

Thanks all.

Going up,

It would be something like this: http://www.youtube.com/watch?v=aHEXr0t5TDI

just not as fancy and not 3D, if it helps as of right now I’m using planes with images painted on as the tokens.

Last bump, then off to cry in corner :frowning:

no error?

No compile errors, the platform allows me to play the game, what happens is something along these lines:

Press play and the game starts
Click a token and drag it to create a clone token
Able to create 1 - 2 tokens
After 1 - 2 clone tokens, the drag/drop functionality freezes up and I’m unable to create additional tokens.

I’m trying to do a work around with instantiate but I’m unsure if I can apply the same drag/drop functionality or how I would spawn objects.

do you want to just instatiate a object when you mouse up or the object you drag?
maybe you should check before you instatiate the object of the objects is acually something.

if(dropObject!=null)
var newToken = Instantiate( dropObject, origPos, transform.rotation );

I’m trying to do it with a drag motion

the movie you added actualy looks like this:

when a OnGUI button is pressed make object of that type at mouse position
update it’s position untill the mouse button is released
stop updating the posision

Yeah, that you-tube video was the closest thing I could find to what I wanted but it doesn’t show everything

That gives a better illustration (I hope), basically I got a set of tokens off to one side when a player clicks and drags any of those given tokens they create a clone token (there is no limit to the number of tokens that they can create) and with that clone token they can make moves on the board, only when a player clicks and drags the side tokens do they make a clone token, a clone token cannot be used to create another clone token.

can you upload a webplayer of how it works now?