1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

Collision detection (2d) with clone

Discussion in 'Physics' started by tiremo, Jun 19, 2017 at 9:04 PM.

  1. tiremo

    tiremo

    Joined:
    Jun 13, 2017
    Posts:
    1
    Hello,

    I struggle a few days now with this problem. In my Player script, the sphere gets instatiatet everytime I press the spacebar. This works pretty good.
    And now, when I shoot another one, I want to check the collision between them. They have both a property called "sphereVal" and the value is random.
    The problem now is, when they collide, both of them trigger the OnCollisionEnter2D.

    Lets say the first sphere fired has the value 10 and the other one 20. When 10 hits 20 it Outputs "less" and "greater" at the same time because they "looking for each other".

    I think ist because they have the same GameTag but I have no clue how to deal with that.

    My goal is, when I shoot a sphere, it should only trigger the collision from "one perspective", if that makes sense.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class sphere: MonoBehaviour {
    6.     public Rigidbody2D rb;
    7.     public int speed;
    8.     public int sphereVal;
    9.     // Use this for initialization
    10.     void Start () {
    11.         rb = GetComponent<Rigidbody2D>();
    12.         rb.tag = "sphere";
    13.         kugelZahl = Random.Range(1,100);
    14.         speed = 6;
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.             float moveVertical = Input.GetAxisRaw("Vertical");
    21.             Vector3 toTop = new Vector3(0, moveVertical + 0.5f, 0.0f);
    22.             rb.velocity = toTop * speed;
    23.     }
    24.  
    25.     private void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.        
    28.         if (collision.gameObject.tag == "sphere" || collision.gameObject.tag == "background")
    29.         {
    30.            
    31.            
    32.             rb.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX;
    33.             int a = collision.gameObject.GetComponent<kugel>().kugelZahl;
    34.            
    35.             if(sphereVal< collision.gameObject.GetComponent<kugel>().sphereVal)
    36.             {
    37.                
    38.                 Debug.Log("less");
    39.             }
    40.             if (sphereVal> collision.gameObject.GetComponent<sphere>().sphereVal)
    41.             {
    42.                 Debug.Log("greater");
    43.             }
    44.  
    45.         }
    46.     }
    47. }
    Greetings from Germany! (Yep, this explains my english)