Sprites overlap Unity C #

2

Hello I am developing a simulator in unity the problem that I have is with the Sprites that would be being tools in the game / simulator, in this case I must use about 8 sprites that simulate being objects which I have to take them and drag them to another object all this in a "touch" for SmartPhones, what happens is that when I take a sprite and pass on another sprite they are absorbed and then I can not separate them becoming a bug. then I show them the code I use for the movement hopefully can help to know what to add to solve this.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class movimiento : MonoBehaviour {


    public float speed = 0.1F; 
    public bool calor = false;

    // Use this for initialization
    void Start () 
    {

    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.touchCount > 0)
            print(Input.touchCount);

        //Gets the world position of the mouse on the screen        
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint( Input.mousePosition );

        //Checks whether the mouse is over the sprite
        bool overSprite = this.GetComponent<SpriteRenderer>().bounds.Contains( mousePosition );

        //If it's over the sprite
        if (overSprite)
        {
            //If we've pressed down on the mouse (or touched on the iphone)
            if (Input.GetButton("Fire1"))
            {
                //Set the position to the mouse position
                this.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                    Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
                    0.0f);
            }
        }
    }
    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "cabeza")
            calor = true;
            Debug.Log ("ouch");
            Debug.Log (calor);
    }


}
    
asked by Nicolas Rudisky 03.08.2016 в 23:57
source

2 answers

2

In the Sprite component, you have a field called "Sorting Order" and under "Order in Layer". These two fields allow you to control which sprite should be seen ahead of the other.

    
answered by 21.05.2017 / 16:44
source
1

I'm not sure what you're referring to with "being absorbed", a screenshot or a video of the bug is good, but from what I understand it can be two things.

The first is that the two sprites overlap and start to see each other on a random basis, that is solved with the order of the layers in 2D for Unity Unity Sorting layers .

If it's not the one above, what I understand, again a capture or video of the bug would be fine, is that you touch a sprite and if you move it over another sprite then you move both sprites and as they stayed together then you no longer You can re-select only one.

If this is the problem, the solution is simple. When you play a sprite you set a flag to true, this flag knows whether or not we play an object and if you already played an object then the raycast has no effect on the others or failing to fire. Something like that

if(!bTengoSprite)
{
  SeleccionObjetoConRaycast();
}

Remember that when you remove the touch from the screen you must set the flag to false so that it lets you re-catch another object.

I hope it helps. Greetings.

    
answered by 05.08.2016 в 19:44