Enemy

PHOTO EMBED

Mon Dec 09 2024 09:02:26 GMT+0000 (Coordinated Universal Time)

Saved by @iliavial

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed;
    public Vector3 moveOffset;
    private Vector3 targetPos;
    private Vector3 startPos;


    // Start is called before the first frame update
    void Start()
    {
        startPos = transform.position;
        targetPos = startPos;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
        if (transform.position == targetPos)
        {
            if (startPos == targetPos)
            {
                targetPos = startPos + moveOffset;

            }
            else
            {
                targetPos = startPos;

            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            other.GetComponent<PlayerController>().GameOver();
        }

    }


}
content_copyCOPY