Vitor Presa
Vitor Presa

Reputation: 51

Grab and drop object

I'm trying new things and I'm stuck to find a way to drag and drop objects. My player is a square and he have a hand attached. Here is an example:

Example

That red thing in the arm is the "hand", when I press shift it turns green. I made the detection like a ground check. Here is the code:

void Update () {
        touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);
        mao1.SetBool("Ligado", ligado);

        if (Input.GetKey(KeyCode.LeftShift)) {
            ligado = true;
        } else {
            ligado = false;
        }        
    }

The touchDetect is working fine, because he turns to "true" when touches the box:

Example2

My question is: I don't know how to put in the script that I want him to grab the object and drop when I want to. If I need to use Raycast, how would the code looks like?

Upvotes: 2

Views: 1587

Answers (2)

James Hogle
James Hogle

Reputation: 3040

In order to "grab" an object in unity, simply set the transform.parent of the gameobject you wish to grab, to the transform of the gameobject you wish to grab it with.

For example, in your code you are using Physics2D.OverlapCircle which returns a Collider2D object. You can use that collider to grab on to your gameobject.

Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);

if (Input.GetKey(KeyCode.LeftShift) && touch != null) 
{
    //grab on to the object
    touch.gameObject.transform.parent = this.transform;

    //if your box has a rigidbody on it,and you want to take direct control of it
    //you will want to set the rigidbody iskinematic to true.
   GetComponent<Rigidbody2D>().isKinematic = true;
} 
else if( touch != null)
{
    //let the object go
    touch.gameObject.transform.parent = null;

    //if your object has a rigidbody be sure to turn kinematic back to false
    GetComponent<Rigidbody2D>().isKinematic = false;
} 

Upvotes: 1

Rasa Mohamed
Rasa Mohamed

Reputation: 892

Just place a GameObject with SprieRender component attach it to hand tip.

Put this script to that qube and follow what I commented

using UnityEngine;
using System.Collections;

public class collid : MonoBehaviour {

// Use this for initialization

public Sprite mySprite; // Put Here that Qube Sprite
public GameObject Cursorimg; // Put the gameobject here from hierarchy

void Start () {
    mySprite = GetComponent<SpriteRenderer>().sprite;
}
// Update is called once per frame
void Update () {

    if(Collide conditions) {
     Debug.Log(" Collide ");
     Cursorimg.GetComponent<SpriteRenderer>().sprite = mySprite;
     Cursorimg.GetComponent<SpriteRenderer>().sortingOrder = 3;// U change based on your option
    }
    if(DropCondition)
    {
     Debug.Log("Drop");
     Cursorimg.GetComponent<SpriteRenderer>().sprite =null;
    }
}

If this not works Here is a script that gameobject with Sprite will follow mouse position,

    Vector3 pos = Input.mousePosition;
    pos.z = Cursorimg.transform.position.z - Camera.main.transform.position.z;
    Cursorimg.transform.position = Camera.main.ScreenToWorldPoint(pos);

this code follows in update and Cursorimg is Gameobject

I think This would help you, Let me know further

Upvotes: 1

Related Questions