Search Unity

Two object combine each other but dont move?

Discussion in 'Scripting' started by svsosm, Nov 16, 2020.

  1. svsosm

    svsosm

    Joined:
    Jul 1, 2020
    Posts:
    2
    this is my SquareObject.cs

    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         moveSpawnObject();
    5.  
    6.     }
    7.  
    8.     void moveSpawnObject()
    9.     {
    10.         if (!isSquare && rotator.isClicked)
    11.         {
    12.             rb.MovePosition(rb.position + Vector2.up * speed * Time.deltaTime);
    13.         }
    14.  
    15.  
    16.         spawner.createSpawnObject();
    17.  
    18.     }
    19.  
    20.  
    21.  
    22.     private void OnTriggerEnter2D(Collider2D collision)
    23.     {
    24.         Debug.Log("game: " + gameObject.tag + " col: " + collision.tag);
    25.         //if collision tag equals gameobject tag, we compound two object
    26.         if (collision.CompareTag(gameObject.tag))
    27.         {
    28.  
    29.  
    30.             if (collision.transform.CompareTag("2"))
    31.             {
    32.                 Destroy(collision.gameObject);
    33.                 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[1];
    34.                 gameObject.tag = (int.Parse(collision.tag) * 2).ToString();
    35.                 Score.score += int.Parse(collision.tag);
    36.             }
    37.             else if (collision.transform.CompareTag("4"))
    38.             {
    39.                 Destroy(collision.gameObject);
    40.                 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[2];
    41.                 gameObject.tag = (int.Parse(collision.tag) * 2).ToString();
    42.                 Score.score += int.Parse(collision.tag);
    43.  
    44.             }
    45.             else if (collision.tag == "8")
    46.             {
    47.                 Destroy(collision.gameObject);
    48.                 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[3];
    49.                 gameObject.tag = (int.Parse(collision.tag) * 2).ToString();
    50.                 Score.score += int.Parse(collision.tag);
    51.  
    52.             }
    53.             else if (collision.tag == "16")
    54.             {
    55.                 Destroy(collision.gameObject);
    56.                 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[4];
    57.                 gameObject.tag = (int.Parse(collision.tag) * 2).ToString();
    58.                 Score.score += int.Parse(collision.tag);
    59.  
    60.             }
    61.             else if (collision.tag == "32")
    62.             {
    63.                 Destroy(collision.gameObject);
    64.                 gameObject.GetComponent<SpriteRenderer>().sprite = sprites[5];
    65.                 gameObject.tag = (int.Parse(collision.tag) * 2).ToString();
    66.                 Score.score += int.Parse(collision.tag);
    67.  
    68.             }
    69.         }else if (!collision.CompareTag(gameObject.tag))
    70.         {
    71.  
    72.             //if not equals collision tag and gameobject tag, we attach each other.
    73.             transform.SetParent(collision.transform);
    74.             gameObject.name = gameObject.tag;
    75.             isSquare = true;
    76.             spawner.isHas = false;
    77.             rotator.isClicked = false;
    78.  
    79.         }
    80.  
    this is Spawner.cs

    Code (CSharp):
    1.  
    2. public void createSpawnObject()
    3.     {
    4.         if(!isHas)
    5.         {
    6.             chance = Random.Range(0, 100); //create random number
    7.             if (chance < 5)
    8.             {
    9.                 //spawner.spawnSquare(2); //8 prefabs
    10.                 Instantiate(squarePrefab[2], transform.position, transform.rotation);
    11.  
    12.             }
    13.             else if (chance > 90)
    14.             {
    15.                 //spawner.spawnSquare(1); //4 prefabs
    16.                 Instantiate(squarePrefab[1], transform.position, transform.rotation);
    17.  
    18.             }
    19.             else
    20.             {
    21.                 //spawner.spawnSquare(0); //2 prefabs
    22.                 Instantiate(squarePrefab[0], transform.position, transform.rotation);
    23.  
    24.             }
    25.             isHas = true;
    26.         }
    27.  
    28.  
    29.     }
    [EDITED]
    What do we want to do?
    When we press the "A" , the object in the middle of the screen will turn 90 degrees to the right. When press "D", it will turn 90 degrees to the left. What we want to do is initially create a spawn at the spawn point. When we press the "A" or "D" , the spawn object moves towards the middle object. All objects have their own tags. If the tags are the same, it should merge, otherwise it will stick to the object it hit. Let it spawn again after sticking to the object.

    if two object same tag, they combine each other but dont move. But we are faced with a problem like image. We have Rotator.cs and when i click button, isClicked = true. How are we solve this?
     

    Attached Files:

    Last edited: Nov 17, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You've stated what is happening, but I have read this 3 times and don't see where you state what you want to happen instead.
     
    neginfinity likes this.
  3. svsosm

    svsosm

    Joined:
    Jul 1, 2020
    Posts:
    2
    Sorry, i explain. When we press the "A" , the object in the middle of the screen will turn 90 degrees to the right. When press "D", it will turn 90 degrees to the left. What we want to do is initially create a spawn at the spawn point. When we press the "A" or "D" , the spawn object moves towards the middle object. All objects have their own tags. If the tags are the same, it should merge, otherwise it will stick to the object it hit. Let it spawn again after sticking to the object.