Search Unity

[HELP THE PIG] 2D Platformer Game

Discussion in 'Getting Started' started by SpiderPig1660, Aug 22, 2020.

  1. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    Hi, I'm very new to Unity and I want to make a 2D Platformer Game! I'm trying to make an enemy behaviour script, but the enemy doesn't follow the player when in range and it doesn't attack the player (It can move around on certain point, it just can't follow the player).

    Here the script:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Collections.Specialized;
    5. using System.Runtime;
    6. using System.Security.Cryptography;
    7. using System.Threading;
    8. using UnityEngine;
    9.  
    10. public class EnemyBehaviour : MonoBehaviour
    11. {
    12.  
    13.     #region Public Varaibles
    14.     public float attackDistance;
    15.     public float moveSpeed;
    16.     public float timer; //Timer for cooldown
    17.     public Transform leftLimit;
    18.     public Transform rightLimit;
    19.     [HideInInspector] public Transform target;
    20.     [HideInInspector] public bool inRange;
    21.     public GameObject hotZone;
    22.     public GameObject triggerArea;
    23.     #endregion
    24.  
    25.     #region Private Variables
    26.     private Animator anim;
    27.     private float distance;
    28.     private bool attackMode;
    29.     private bool cooling;
    30.     private float intTimer;
    31.     #endregion
    32.  
    33.     void Awake()
    34.     {
    35.         SelectTarget();
    36.         intTimer = timer;
    37.         anim = GetComponent<Animator>();
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.         if (!attackMode)
    43.         {
    44.             Move();
    45.         }
    46.  
    47.         if(!InsideofLimits() && !inRange && !anim.GetCurrentAnimatorStateInfo(0).IsName("Attack"))
    48.         {
    49.             SelectTarget();
    50.         }
    51.  
    52.         if (inRange)
    53.         {
    54.             EnemyLogic();
    55.         }
    56.     }
    57.  
    58.     void EnemyLogic()
    59.     {
    60.         distance = Vector2.Distance(transform.position, target.position);
    61.  
    62.         if (distance > attackDistance)
    63.         {
    64.             StopAttack();
    65.         }
    66.         else if (attackDistance >= distance && cooling == false)
    67.         {
    68.             Attack();
    69.         }
    70.  
    71.         if (cooling)
    72.         {
    73.             Cooldown();
    74.             anim.SetBool("Attack", false);
    75.         }
    76.     }
    77.  
    78.     void Move()
    79.     {
    80.         anim.SetBool("canWalk", true);
    81.  
    82.         if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Attack"))
    83.         {
    84.             Vector2 targetPosition = new Vector2(target.position.x, transform.position.y);
    85.  
    86.             transform.position = Vector2.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
    87.         }
    88.     }
    89.  
    90.     void Attack()
    91.     {
    92.         timer = intTimer;
    93.         attackMode = true;
    94.  
    95.         anim.SetBool("canWalk", false);
    96.         anim.SetBool("Attack", true);
    97.     }
    98.  
    99.     void Cooldown()
    100.     {
    101.         timer -= Time.deltaTime;
    102.  
    103.         if (timer <= 0 && cooling && attackMode)
    104.         {
    105.             cooling = false;
    106.             timer = intTimer;
    107.         }
    108.     }
    109.  
    110.     void StopAttack()
    111.     {
    112.         cooling = false;
    113.         attackMode = false;
    114.         anim.SetBool("Attack", false);
    115.     }
    116.  
    117.     public void TriggerCooling()
    118.     {
    119.         cooling = true;
    120.     }
    121.  
    122.     private bool InsideofLimits()
    123.     {
    124.         return transform.position.x > leftLimit.position.x && transform.position.x < rightLimit.position.x;
    125.     }
    126.  
    127.     public void SelectTarget()
    128.     {
    129.         float distanceToLeft = Vector2.Distance(transform.position, leftLimit.position);
    130.         float distanceToRight = Vector2.Distance(transform.position, rightLimit.position);
    131.    
    132.         if(distanceToLeft > distanceToRight)
    133.         {
    134.             target = leftLimit;
    135.         }
    136.         else
    137.         {
    138.             target = rightLimit;
    139.         }
    140.  
    141.         Flip();
    142.     }
    143.  
    144.     public void Flip()
    145.     {
    146.         Vector3 rotation = transform.eulerAngles;
    147.         if(transform.position.x > target.position.x)
    148.         {
    149.             rotation.y = 180f;
    150.         }
    151.         else
    152.         {
    153.             rotation.y = 0f;
    154.         }
    155.  
    156.         transform.eulerAngles = rotation;
    157.     }
    158. }
    159.  
    160.  
     
  2. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    If anyone can help me fix the script or show me how to make enemy damage player health that would be very appreciate :D
     
    Last edited: Aug 22, 2020
  3. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,076
    If I remember correctly, Vector3 is for 3d.
    Unless something was changed in Unity, and I strangely didn't know about it.:p
     
    SpiderPig1660 likes this.