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

Opinion on enemy movement script for 2D platformer game

Discussion in 'Scripting' started by CristianHG, Jun 9, 2016.

  1. CristianHG

    CristianHG

    Joined:
    Aug 22, 2015
    Posts:
    2
    Hello Unity Community, this is my first post on the forums.

    I have made a small script for basic enemy movement for a 2D Super Mario Bros-like platformer that uses mainly Raycasting to detect hits and to decide whether we should change direction or not.

    The script has two modes, MovementTypes.Floor which just uses a coroutine to change directions every X amount of seconds and MovementTypes.Platform which uses Raycasts to determine whether or not to change directions by offsetting the origin of the cast.

    It then uses three more raycasts to determine whether the enemy is damaging the player (Hit left and right) or whether the player has damaged the enemy (Hit from top).

    And well, basically, I just wanted to get some opinion on it, maybe some constructive criticism.

    Full script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class EnemyMovement : MonoBehaviour {
    6.  
    7.     public enum MovementTypes
    8.     {
    9.         Platform,
    10.         Floor
    11.     }
    12.  
    13.     public enum Directions {
    14.         Left,
    15.         Right
    16.     }
    17.  
    18.     public Directions Direction = Directions.Left;
    19.     public MovementTypes GroundType = MovementTypes.Platform;
    20.  
    21.     public int EnemySpeed = 300;
    22.     public float SwitchInterval = 10f;
    23.  
    24.     bool alive = true;
    25.  
    26.     void Start()
    27.     {
    28.         if ( GroundType == MovementTypes.Floor )
    29.             StartCoroutine ("SwitchDirection");
    30.     }
    31.  
    32.     void FixedUpdate ()
    33.     {
    34.         // Death
    35.  
    36.         RaycastHit2D topCheck = Physics2D.Raycast(new Vector2 (transform.position.x + 0.5f, transform.position.y + 1.3f), Vector2.up * 0.1f, 0.1f);
    37.  
    38.         if ( topCheck.collider != null )
    39.         {
    40.             // Enemy is hit from top
    41.  
    42.             if ( topCheck.collider.tag == "Player" )
    43.                 alive = false;
    44.         }
    45.  
    46.         // Everything else
    47.  
    48.         if ( alive )
    49.         {
    50.             if (Direction == Directions.Left)
    51.             {
    52.                 if ( GroundType == MovementTypes.Platform )
    53.                 {
    54.                     RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x - 0.1f, transform.position.y), Vector2.down * 0.2f, 0.2f);
    55.  
    56.                     if ( hit.collider != null )
    57.                         transform.Translate (new Vector3 (((EnemySpeed / 50) * -1) * Time.fixedDeltaTime, 0, 0));
    58.                     else
    59.                         Direction = Directions.Right;
    60.                 }
    61.                 else
    62.                     transform.Translate (new Vector3 (((EnemySpeed / 50) * -1) * Time.fixedDeltaTime, 0, 0));
    63.             }
    64.             else
    65.             {
    66.                 if ( GroundType == MovementTypes.Platform )
    67.                 {
    68.                     RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x + 1.3f, transform.position.y), Vector2.down * 0.2f, 0.2f);
    69.  
    70.                     if ( hit.collider != null )
    71.                         transform.Translate (new Vector3 ((EnemySpeed / 50) * Time.fixedDeltaTime, 0, 0));
    72.                     else
    73.                         Direction = Directions.Left;
    74.                 }
    75.                 else
    76.                     transform.Translate (new Vector3 (((EnemySpeed / 50)) * Time.fixedDeltaTime, 0, 0));
    77.             }
    78.  
    79.             // Hitting
    80.  
    81.             RaycastHit2D leftCheck = Physics2D.Raycast (new Vector2 (transform.position.x, transform.position.y + 0.5f), Vector2.left * 0.1f, 0.1f);
    82.             RaycastHit2D rightCheck = Physics2D.Raycast (new Vector2 (transform.position.x + 1.3f, transform.position.y + 0.5f), Vector2.right * 0.1f, 0.1f);
    83.  
    84.             if ( leftCheck.collider != null )
    85.             {
    86.                 if (leftCheck.collider.tag == "Player")
    87.                 {
    88.                     PlayerControl sc = leftCheck.collider.GetComponent<PlayerControl> ();
    89.                     sc.changePlayerStatus ("Dead");
    90.                 }
    91.             }
    92.  
    93.             if ( rightCheck.collider != null )
    94.             {
    95.                 if (rightCheck.collider.tag == "Player")
    96.                 {
    97.                     PlayerControl sc = rightCheck.collider.GetComponent<PlayerControl> ();
    98.                     sc.changePlayerStatus ("Dead");
    99.                 }
    100.             }
    101.         }
    102.     }
    103.     IEnumerator SwitchDirection()
    104.     {
    105.         while ( alive )
    106.         {
    107.             if ( Direction == Directions.Left )
    108.                 Direction = Directions.Right;
    109.             else
    110.                 Direction = Directions.Left;
    111.  
    112.             yield return new WaitForSeconds (SwitchInterval);
    113.         }
    114.     }
    115. }


    Thank you for checking out my thread :)
     
    Last edited: Jun 9, 2016
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Instead of using raycast to check how damage should occur, I would personally set up a single trigger collider for the enemy. Use OnTriggerEnter and get the direction the player is moving to handle damage. Other than that it looks good.
     
  3. CristianHG

    CristianHG

    Joined:
    Aug 22, 2015
    Posts:
    2
    Oh, that sounds like a good idea! But how would I go about detecting the direction in which the player is colliding with the enemy?