Search Unity

Feedback 2D Enemy Script

Discussion in 'Scripting' started by viiikay, Apr 17, 2022.

  1. viiikay

    viiikay

    Joined:
    Jul 14, 2020
    Posts:
    3
    Hello everyone! I'm helping create a game for a college final and am not very well versed in C# or computer programming as a whole. I created an enemy script that is supposed to trigger an enemy attack when the player is close. For reference, I used an enemy tutorial from inScope Studios on Youtube and modified it to use OnTriggerEnter rather than OnCollider.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyScript : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Animator animator;
    9.  
    10.     private Transform enemy;
    11.  
    12.     private GameObject Enemy;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.  
    24.     }
    25.  
    26.     public void OnTriggernEnter(string enemytag, GameObject other)
    27.     {
    28.         if (enemytag == "Enemy" && other.tag == "Player")
    29.         {
    30.             other.GetComponent<PlayerMoveScript>().TakeHit();
    31.         }
    32.     }
    33.  
    34.     public void StopAttack()
    35.     {
    36.         animator.SetBool("Attack", false);
    37.     }
    38.     public void Attack()
    39.     {
    40.         GameObject go = Instantiate(Enemy, enemy.position, Quaternion.identity);
    41.         Vector3 direction = new Vector3(transform.localScale.x, 0);  
    42.  
    43.     }
    44.  
    45. }
    The script unfortunately does not work and I am not sure how to fix it.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I'd be curious to see a link to that because if it's as you say, it's bad information.

    As you sure this wasn't just their custom function that was called from the real OnTriggerEnter? Yours is named "OnTriggernEnter" (extra n).
     
  4. viiikay

    viiikay

    Joined:
    Jul 14, 2020
    Posts:
    3
    Thank you for letting me know! I fixed it to match and added a line of code that calls for a comparison of the game object tags to trigger an enemy hit but I don't think it's doing anything.

    Code (CSharp):
    1.  public void OnTriggerEnter(Collider other)
    2.     {
    3.         if (gameObject.tag == "Enemy" && other.tag == "Player")
    4.         {
    5.             other.GetComponent<PlayerMoveScript>().TakeHit();
    6.         }
    7.     }
     
  5. viiikay

    viiikay

    Joined:
    Jul 14, 2020
    Posts:
    3
    That was actually my mistake! I didn't realize I had added an extra n.
    And here is the link:


    I tried doing it his way, but the addition of an interface seemed too complicated for what I'm trying to create since non of my enemies fire projectiles and have very simple attacks.
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Yes, I don't see why you're be following this tutorial really as it adds complications and I get a sense you've not followed the basic 2D physics tutorials (I could be wrong) on Unity Learn for instance. There seems to be additional confusion going on here. You're using the 3D physics callbacks, not the 2D physics callbacks. All 2D callbacks use the "2D" suffix the same as the tutorial shows.