Opinion on enemy movement script for 2D platformer game

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:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class EnemyMovement : MonoBehaviour {

    public enum MovementTypes
    {
        Platform,
        Floor
    }

    public enum Directions {
        Left,
        Right
    }

    public Directions Direction = Directions.Left;
    public MovementTypes GroundType = MovementTypes.Platform;

    public int EnemySpeed = 300;
    public float SwitchInterval = 10f;

    bool alive = true;

    void Start()
    {
        if ( GroundType == MovementTypes.Floor )
            StartCoroutine ("SwitchDirection");
    }

    void FixedUpdate ()
    {
        // Death

        RaycastHit2D topCheck = Physics2D.Raycast(new Vector2 (transform.position.x + 0.5f, transform.position.y + 1.3f), Vector2.up * 0.1f, 0.1f);

        if ( topCheck.collider != null )
        {
            // Enemy is hit from top

            if ( topCheck.collider.tag == "Player" )
                alive = false;
        }

        // Everything else

        if ( alive )
        {
            if (Direction == Directions.Left)
            {
                if ( GroundType == MovementTypes.Platform )
                {
                    RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x - 0.1f, transform.position.y), Vector2.down * 0.2f, 0.2f);

                    if ( hit.collider != null )
                        transform.Translate (new Vector3 (((EnemySpeed / 50) * -1) * Time.fixedDeltaTime, 0, 0));
                    else
                        Direction = Directions.Right;
                }
                else
                    transform.Translate (new Vector3 (((EnemySpeed / 50) * -1) * Time.fixedDeltaTime, 0, 0));
            }
            else
            {
                if ( GroundType == MovementTypes.Platform )
                {
                    RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x + 1.3f, transform.position.y), Vector2.down * 0.2f, 0.2f);

                    if ( hit.collider != null )
                        transform.Translate (new Vector3 ((EnemySpeed / 50) * Time.fixedDeltaTime, 0, 0));
                    else
                        Direction = Directions.Left;
                }
                else
                    transform.Translate (new Vector3 (((EnemySpeed / 50)) * Time.fixedDeltaTime, 0, 0));
            }

            // Hitting

            RaycastHit2D leftCheck = Physics2D.Raycast (new Vector2 (transform.position.x, transform.position.y + 0.5f), Vector2.left * 0.1f, 0.1f);
            RaycastHit2D rightCheck = Physics2D.Raycast (new Vector2 (transform.position.x + 1.3f, transform.position.y + 0.5f), Vector2.right * 0.1f, 0.1f);

            if ( leftCheck.collider != null )
            {
                if (leftCheck.collider.tag == "Player")
                {
                    PlayerControl sc = leftCheck.collider.GetComponent<PlayerControl> ();
                    sc.changePlayerStatus ("Dead");
                }
            }

            if ( rightCheck.collider != null )
            {
                if (rightCheck.collider.tag == "Player")
                {
                    PlayerControl sc = rightCheck.collider.GetComponent<PlayerControl> ();
                    sc.changePlayerStatus ("Dead");
                }
            }
        }
    }
    IEnumerator SwitchDirection()
    {
        while ( alive )
        {
            if ( Direction == Directions.Left )
                Direction = Directions.Right;
            else
                Direction = Directions.Left;

            yield return new WaitForSeconds (SwitchInterval);
        }
    }
}

Thank you for checking out my thread :slight_smile:

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.

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?