basic_slime_script.cs 1.84 KB
Newer Older
James D Fonda's avatar
James D Fonda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
using UnityEngine;
using System.Collections;

public class basic_slime_script : MonoBehaviour
{
    private Vector3 player_position;
    private Vector3 slime_position;
    private GameObject player;
    private float x_diff;
    private float y_diff;
    

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        //get position of player character
        player_position = player.transform.position;

        //get position of slime
        slime_position = transform.position;

        //compare to position of slime (self)
        x_diff = player_position.x - slime_position.x;
        y_diff = player_position.y - slime_position.y;
 
        if (Mathf.Abs(x_diff) >= Mathf.Abs(y_diff))
        {
            //if move is valid, then move
            if (x_diff < 0)
            {
                if (World.get((int)(slime_position.x - 1 + 0.5), (int)(slime_position.y + 0.5)).walkable == true)
                    transform.position += Vector3.left;
            }
            if (x_diff > 0)
            {
                if (World.get((int)(slime_position.x + 1 + 0.5), (int)(slime_position.y + 0.5)).walkable == true)
                    transform.position += Vector3.right;
            }
        }
        else
        {
            //if move is valid, then move
            if (y_diff < 0)
            {
                if(World.get((int)(slime_position.x + 0.5), (int)(slime_position.y - 1 + 0.5)).walkable == true)
                    transform.position += Vector3.down;
            }
            if (y_diff > 0)
            {
                if (World.get((int)(slime_position.x + 0.5), (int)(slime_position.y + 1 + 0.5)).walkable == true)
                    transform.position += Vector3.up;
            }
        }
    }
}