1
\$\begingroup\$

This is the code I am using but it's not working. It drags other object on collision:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TileControllerHorizontal : MonoBehaviour
{

    private Vector3 screenPoint;
    private Vector3 offset;
    public bool check;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {

    }

    void OnMouseDown ()
    {
        screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);

        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        transform.name = "draging";

    }

    void OnMouseDrag ()
    {
        if (!check) {
            Vector3 point = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            point.z = gameObject.transform.position.z;
            point.y = gameObject.transform.position.y;
            gameObject.transform.position = point;
        }
    }

    void OnMouseUp ()
    {
        check = false;
        transform.name = "Tile";

    }

    void OnCollisionEnter2D (Collision2D col)
    {
        Debug.Log (col.gameObject.tag);
        if (col.gameObject.tag == "Player" && transform.name == "draging") {
            check = true;
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Sorry, what isn't working, what specifically isn't it doing? I'm guessing you want it to stop moving the object you are dragging when it collides with another? \$\endgroup\$ – Pheonix2105 Jun 12 '18 at 15:27
1
\$\begingroup\$

I made a little sample project of how I would implement this, as a component you can simply attach to any gameobject you want to be made draggable.

I will include the code for the mono behaviour here too, each sprite you want to be able to collide needs a BoxCollider2D and a RigidBody2D, if you don't want physics to apply to the objects make sure to set GravityScale to 0 and Mass to something really low like 0.00001 on the rigidbody2d components.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragComponent : MonoBehaviour {

//try to explicity declare variables when you can so you're always sure what a variables state is at any given line
public bool dragging = false;

// Update is called once per frame
void Update ()
{

    CheckForClicks();

    //if this is currently being dragged
    if (this.dragging)
    {
        //create a variable to hold the mouse position just because it looks clearer and easier to read
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        //now we create a second vector3 as we don't want the sprite z axis to match the mouse position only on X and Y we want to stay on the same z axis
        Vector3 position = new Vector3(mousePosition.x, mousePosition.y, 1);
        //set its position equal to the vector3 we just created
        transform.position = position;
    }
}

private void CheckForClicks()
{
    //if left click
    if (Input.GetMouseButtonDown(0))
    {
        //get mouse position as world position
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        //ray cast and whatever was hit gets returned
        RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero);

        //did we hit something with a collider?
        if (hit.collider != null)
        {
            //is the collider we hit the same as the whichever object is currently running this script?
            if (hit.collider.gameObject == gameObject)
            {
                //yes it is so set draggable to true, which lets the update function lock its position to the mouse position
                this.dragging = true;
            }

        }

    }
    //if we press right click stop draggin this object
    else if(Input.GetMouseButtonDown(1))
    {
        //and if whatever is running this script is currently being dragged
        if (this.dragging)
        {
            //set dragging to false
            this.dragging = false;
        }
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{

    Debug.Log(collision);
    //if the game object we collided with is not equal to the current game object (so anything other than itself)
    if (collision.gameObject != gameObject)
    {
        //turn dragging for this object off because we hit something
        this.dragging = false;
    }
    }

}

https://www.dropbox.com/s/k464kgvewg88owl/dragging_demo.unitypackage?dl=0

Notice in the example project the draggable script is added to both objects and you can drag either one into the other and it will stop moving.

You could change in the collision function

        //turn dragging for this object off because we hit something
        this.dragging = false;

to

        //turn dragging for this object off because we hit something
        Destroy(collison.gameObject)
        this.dragging = false;

and it will now destroy whatever it hits.

You could go one further and move the logic of what happens when an object is collided with to its own component script that way you can have different behaviours for different tiles.

Adding this to a gameObject that has a Collider2D and RigidBody2D will make the obejct destroy itself when hit, you can easily expand this so other objects may become larger when hit by creating a different component and adding it to the object.

public class BreakableComponent : MonoBehaviour
{

private void OnCollisionEnter2D(Collision2D collision)
{
    //if the game object we collided with is not equal to the current game object (so anything other than itself)
    if (collision.gameObject != gameObject)
    {
        //KILL THIS OBJECT
        Destroy(gameObject);
    }
}

}

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.