Search Unity

RayCast Collision between 2 game objects (of the same prefab)

Discussion in 'Scripting' started by sjefke31, Feb 13, 2012.

  1. sjefke31

    sjefke31

    Joined:
    Feb 13, 2012
    Posts:
    10
    Right now I'm working on a game that checks several collisions at once, but all this is realised within one and the same script for one type of each game object.

    So right now I have one script that checks collision between:
    - 1 PLAYER and 1 BOX

    Basically I dragged 1 PLAYER and 1 BOX to the game world. I attached a script to the PLAYER that sends out RayCasts to check for collision with the collider of that BOX. Now when the RayCast of the PLAYER hits the Collider of the BOX, the BOX should move. All this works extremely well and I'm happy with the result.

    Now the problem is, if I drag ANOTHER BOX to the game world and the RayCast of the PLAYER hits one of the Colliders of these TWO BOXES, they BOTH get moved at the same time to the same direction. This is quite logical, as the RayCast script on the PLAYER tells the BOX to move to a specific coordinate.

    Now the question is, how can I make my RayCast check every box seperated from eachother? So no matter how many boxes I add to the game world, every box should have their OWN collision.

    If someone can please give me a headsup where to begin looking, I am incredibly thankful!
     
  2. sjefke31

    sjefke31

    Joined:
    Feb 13, 2012
    Posts:
    10
    Anybody that can help me here? Please?
     
  3. BigBoy

    BigBoy

    Joined:
    Jan 5, 2012
    Posts:
    91
    so in the ray cast function you give a First box tag name and check it.........

    that time the first box only move......

    one time i did this same thing i thought......

    do it hope it ill work..
     
  4. sjefke31

    sjefke31

    Joined:
    Feb 13, 2012
    Posts:
    10
    I'm sorry, but what do you mean? I don't understand what you were trying to say..
     
  5. jsipich

    jsipich

    Joined:
    Feb 3, 2012
    Posts:
    87
    If he instantiates another prefab the same tag will appear again - back to square 1 unless he plans on having every box existing in the game world and not instantiating during playtime.

    OP - check for collision based on the GameObject, the parent object of the collider being intersected by the raycast. It sounds like you are using a tag as an identifier.
     
  6. sjefke31

    sjefke31

    Joined:
    Feb 13, 2012
    Posts:
    10
    Yes, I am using tags. My reason for using tags is because the movable boxes all have the same function but should move independently of eachother upon hit. Maybe working with a property is the solution to this?
     
  7. Wmellema

    Wmellema

    Joined:
    Nov 7, 2011
    Posts:
    42
    How do you move the boxes? Could you post the movement part here?
     
  8. sjefke31

    sjefke31

    Joined:
    Feb 13, 2012
    Posts:
    10
    I thought it would be smart to realise this with properties, but I'm having issues with properties in the other thread.

    Anyways, to still answer your question:
    Code (csharp):
    1.  
    2. if (Physics.Raycast(transform.position + new Vector3(0, 0.1f, -0.15f), checkBoxCollisionRight, out hit, distance))
    3.         {
    4.             if (hit.collider.gameObject.tag == "box")
    5.             {
    6.                 addBoxStats.BoxPosition += Vector3.up;
    7.             }
    8.         }
    9.  
    Basically what it does is:
    - If the RayCast hits the Collider of a gameObject with the tag "box" it should move that SPECIFIC box.

    I want it to move only the box that it hits, instead it moves all boxes using this method.
     
    Last edited: Feb 15, 2012
  9. Wmellema

    Wmellema

    Joined:
    Nov 7, 2011
    Posts:
    42
    You have to attach a script for each individual block, and then do something like this:
    Code (csharp):
    1. hit.gameObject.GetComponent(MyScript).position += Vector3.up;
     
  10. sjefke31

    sjefke31

    Joined:
    Feb 13, 2012
    Posts:
    10
    Every block already had the script attached as I gave the script to the prefab. :)

    But that line is quite magical, it works but not optimal. I tried this:
    Code (csharp):
    1.  
    2.     if (Physics.Raycast(transform.position + new Vector3(0, 0.1f, -0.15f), checkBoxCollisionRight, out hit, distance))
    3.             {
    4.                 if (hit.collider.gameObject.tag == "box")
    5.                 {
    6.                     hit.collider.gameObject.GetComponent("BoxStats").transform.position += new Vector3(0, 0, -0.32f);
    7.                 }
    8.             }
    9.  
    That is one way, but not really optimal for me as the player doesn't move along then.