Sunday, July 10, 2022
HomeGame Developmentunity - Destroy all related balls when clicking one of many balls

unity – Destroy all related balls when clicking one of many balls


Screenshot of stacked balls

I need to make a mechanic like this: on clicking one of many related balls, all related balls get destroyed.

Up to now I’ve include the tactic within the script under. Nevertheless, it doesn’t work in any respect.

I attempted storing balls as earlier balls and subsequent balls when a brand new ball enters the collider after which utilizing this linked checklist getting the primary ball and begin deleting from there however I’m not capable of implement that.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public enum BallsType
{
    yellow,
    blue,
    crimson,
    inexperienced
}
public class ConnectedObject
{
    public BallsController connectedBall;
    public GameObject spawnedLine;
}
public class BallsController : MonoBehaviour
{
    public BallsWithColor myColor;
    public GameObject linePrefab;
    public SpriteRenderer myrenderer;
    [SerializeField]non-public ConnectedObject nextObj;
    public ConnectedObject prevObj=new();
    public string prevObjName;
    void Begin()
    {
        BallsWithColor returnedColor = GameManager.occasion.GetRandomColor();
        myColor = new BallsWithColor
        {
            ballSprite =returnedColor.ballSprite,
            ballsType = returnedColor.ballsType,
            ballColor = new Shade
            {
                r=returnedColor.ballColor.r,
                g=returnedColor.ballColor.g,
                b=returnedColor.ballColor.b,
                a=1,
            }
        };
        myrenderer.shade = myColor.ballColor;
    }

    // Replace is known as as soon as per body
    void Replace()
    {
        
    }
    non-public bool connectedObjectHasMe(BallsController controller)
    {
        if (controller.nextObj == null)
            return false;
        if (controller.nextObj.connectedBall == null)
            return false;
        if (controller.nextObj.connectedBall == this)
            return true;
        BallsController nextcontoller = controller;
        whereas(nextcontoller != null)
        {
            if (nextcontoller == this)
                return true;
            if (nextcontoller.nextObj == null)
                return false;
            nextcontoller = nextcontoller.nextObj.connectedBall;
        }

        return false;
    }
    non-public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Ball"))
        {
            BallsController different = collision.GetComponent<BallsController>();
            if (different == null)
                return;
            if (myColor.ballsType != different.myColor.ballsType)
                return;

            //if (!isObjectInList(collision.GetComponent<BallsController>()) )
            if (nextObj==null && !connectedObjectHasMe(different) )
            {
                Vector2 pos = new((remodel.place.x + collision.remodel.place.x) / 2,
                (remodel.place.y + collision.remodel.place.y) / 2);
                Vector3 targ =pos;
                targ.z = 0;
                Vector3 objectPos = remodel.place;
                targ.x -= objectPos.x;
                targ.y -= objectPos.y;

                float angle = Mathf.Atan2(targ.y, targ.x) * Mathf.Rad2Deg;
                GameObject spawnedLine = Instantiate(linePrefab, pos, Quaternion.Euler(new Vector3(0, 0, angle)));
                spawnedLine.GetComponent<SpriteRenderer>().shade = myColor.ballColor;
                nextObj=new ConnectedObject
                {
                    connectedBall = collision.GetComponent<BallsController>(),
                    spawnedLine = spawnedLine
                };
                different.prevObj.connectedBall=this;
                different.prevObjName = this.GetInstanceID().ToString();
            }
        }
    }
    non-public void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Ball"))
        {
            BallsController different = collision.GetComponent<BallsController>();

            if (myColor.ballsType != different.myColor.ballsType)
                return;

            //if (isObjectInList(collision.GetComponent<BallsController>()))
            if (nextObj == null &&!connectedObjectHasMe(different))
            {
                Vector2 pos = new((remodel.place.x + collision.remodel.place.x) / 2,
                (remodel.place.y + collision.remodel.place.y) / 2);
                Vector3 targ = pos;
                targ.z = 0;
                Vector3 objectPos = remodel.place;
                targ.x -= objectPos.x;
                targ.y -= objectPos.y;

                //Off by 90 levels for some causes
                float angle = Mathf.Atan2(targ.y, targ.x) * Mathf.Rad2Deg;
                GameObject spawnedLine = Instantiate(linePrefab, pos, Quaternion.Euler(new Vector3(0, 0, angle)));
                spawnedLine.GetComponent<SpriteRenderer>().shade = myColor.ballColor;
                nextObj = new ConnectedObject
                {
                    connectedBall = collision.GetComponent<BallsController>(),
                    spawnedLine = spawnedLine
                };
                different.prevObj.connectedBall = this;
                different.prevObjName = this.GetInstanceID().ToString();

            }
            else if (nextObj != null)
            {
                //SetLinePos(collision.GetComponent<BallsController>());
                Vector2 pos = new((remodel.place.x + nextObj.connectedBall.remodel.place.x) / 2,
                (remodel.place.y + nextObj.connectedBall.remodel.place.y) / 2);
                Vector3 targ = pos;
                targ.z = 0;
                Vector3 objectPos = remodel.place;
                targ.x -= objectPos.x;
                targ.y -= objectPos.y;
                float angle = Mathf.Atan2(targ.y, targ.x) * Mathf.Rad2Deg;
                nextObj.spawnedLine.remodel.SetPositionAndRotation(pos, Quaternion.Euler(new Vector3(0, 0, angle)));

            }
        }
    }
    non-public void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Ball"))
        {
            BallsController different = collision.GetComponent<BallsController>();

            if (myColor.ballsType != collision.GetComponent<BallsController>().myColor.ballsType)
                return;
            //if (isObjectInList(collision.GetComponent<BallsController>()))
            if (nextObj != null)
            {
                //DestroyObjectInList(collision.GetComponent<BallsController>());
                Destroy(nextObj.spawnedLine);
                nextObj=null;
                different.prevObjName = "";
                different.prevObj.connectedBall = null;
            }
        }
    }
    non-public BallsController GetFirstInTheList(BallsController controller)
    {
        BallsController prev = controller;
        whereas(prev != null)
        {
            Debug.Log(prev.GetInstanceID());
            if (prev.prevObj.connectedBall == null)
            {
                return prev;
            }
            prev = prev.prevObj.connectedBall;
        }
        Debug.Log("Lots is unsuitable");
        return null;
    }
    public void DestroyMe()
    {
        //BallsController firstInList = GetFirstInTheList(this);
        if(nextObj != null)
        {
            if(nextObj.connectedBall != null)
            {
                nextObj.connectedBall.DestroyMe();
            }
        }
        //GameManager.occasion.AddScore(1);
        //Some Results
        Destroy(gameObject);
    }
    public void DestroyOnlyMe()
    {
        if (nextObj != null)
        {
            if (nextObj.connectedBall != null)
            {
                nextObj.connectedBall.DestroyOnlyMe();
            }
        }
        Destroy(gameObject);
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments