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

Most efficient way for hit detection (Unity 3D) and general questions

Discussion in 'Scripting' started by billygamesinc, Dec 13, 2020.

  1. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    I'm currently looking for the most efficient way for hit detection for my game. The hit detection only occurs during an attack and only occurs once per attack for each enemy hit. The current method I'm trying to use is through animator events where I toggle the hit detection to on and off. However, I'm having some issues passing the variable to this enemy script.

    -Could it be that the animator event only passes the variable to my player character?
    -What is the least performance heavy method for hit detection? Changing collider layers, tags, toggling/hiding [weapon] colliders.
    -What are some things I should avoid? i.e. excessive use of Update functions.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class EnemyCollision: MonoBehaviour
    6. {
    7. AudioSource EnemyHurt;
    8. public bool canHarm;
    9. private void Start()
    10. {
    11.     EnemyHurt= GetComponent<AudioSource>();
    12. }
    13. private void OnCollisionEnter (Collision collision)
    14. {
    15.     if(canHarm == true)
    16.     {
    17.     EnemyHurt.Play();
    18.     Debug.Log(canHarm)
    19.      }
    20. }
    21.     public void ToggleHarm()   //This function is toggled inside the animation events.
    22.     {      
    23.         canHarm = !canHarm;
    24.         Debug.Log(canHarm);
    25.     }    
    26. }
     
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    To pass variables between scripts, which are components, you need to use
    GetComponent<scriptName>();
    Scripts are a special component, because all names are unique, unlike (say) AudioSources. So the variable in your script that references it needs to be of type scriptName.

    Put at the top:
    scriptName myScript;
    GameObject objectWithScript;

    in Start:
    myScript = objectWithScript.GetComponent<scriptName>();

    Make a public gameObject and drag the object with the script you want into it. Now you can access all *public* variables and methods in it with

    myScript.variable1 = 50;
    myScript.someMethod();

    Once you see how to pass things around, it becomes an organizational issue: what scripts are going to manage which parts of the overall game logic. I usually have a large master script which handles most things, and smaller scripts on various objects that do internal changes to them (that the master script doesn't need to know about).
     
  3. billygamesinc

    billygamesinc

    Joined:
    Dec 5, 2020
    Posts:
    245
    Thank you very much, I'm using this for my enemy to player script at the moment with the intention of using a master script once I get this to work.

    Would it be possible to use this reliably for multiple objects using the same script for the part below?

    GameObject objectWithScript;