Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need a Collision detection script for my game

Discussion in 'Scripting' started by SamsonSmith, Dec 12, 2020.

  1. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    I am currently working on a game that is based around being a astronaut on mars, trying to dodge meteors that constantly keep falling from the sky. I have been looking at videos for days on collision in unity but nothing has helped so far as what I would like is much different. Currently, my meteors don't have any movement scripts on them as all I needed was a ridged body and a box collider so they can fall and collide with the ground. im just really stuck on finding a working script that I can add to the meteors so when they interact with the astronaut, a game over UI Panel (with I have already designed) becomes enabled.

    Thanks
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Can check for collision with a script on the meteors. Then check if it is the player they are hitting and call a function.

    Code (CSharp):
    1.  void OnCollisionEnter(Collision collision)
    2.     {
    3.          if(collision.gameObject.name == "Player")
    4.          {
    5.              collision.GetComponent<ThePlayerScript>().PublicHitFunction();
    6.          }
    7.     }
     
  3. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    thanks for the script. Should i put it in the void update part?
     
  4. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    Also, what should I add to the script for it to enable a UI?
     
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    No, it's a Unity function that will get called when it happens, so have it outside (on the "main level") of your script, just like Update() is.

    For enabling the UI, make a public gameobject in your script with the correct type (Panel), name it whatever you want in the script, and drag the from the Hierarchy into the slot. Then, in the script, you can use
    panelVariableName.setActive(true);
     
    SamsonSmith likes this.
  6. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    Ok, Thanks!
     
  7. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    This is the script I've got on my meteor so far from your help:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class script : MonoBehaviour
    {
    // Update is called once per frame
    void Update()
    {
    void OnCollisionEnter(Collision collision)
    {
    if (collision.gameObject.name == "Astronaut")
    {

    }
    }
    }
    }

    I have already pre-made my game over screen and disabled it. What piece of code would I have to put so the UI can be enabled on contact from the meteor and the astronaut? The UI is called GameOverScreen.

    Thanks!
     
  8. SamsonSmith

    SamsonSmith

    Joined:
    Nov 3, 2020
    Posts:
    18
    This screenshot should make the script more easy to see
     

    Attached Files:

  9. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You're on the right track. First thing to do is a debug.log statement showing that the collision happened. You should also look into tags for object collisions, not just gameobject names, but this will work here too.
    If the collision is happening, then go to my earlier post and set up the reference to the UI Panel you want to activate/deactivate, and the call to activate it goes in the collision code. Experiment a bit and definitely take some time to look through the C# tutorials.
    Also be sure to use the "code" tags when you post code, it formats it for the web page.
     
  10. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You could make a public reference of the UI you want to activate in the player's script then you would have access to it on the collision with collision.transform.GetComponent<PlayerScript>().PublicReference.