ankoanju / COMP23G2

0 stars 0 forks source link

Boars Script #12

Open ankoanju opened 4 years ago

FortiethAtom4 commented 3 years ago

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

public class Boar : MonoBehaviour { public Transform player; public Transform boar; private Vector2 attackDirection; private float aggroMagnitude; private float aggroRange; // Start is called before the first frame update void Start() { aggroRange = 6; }

// Update is called once per frame
void Update()
{
    if(Vector2.Distance(player.transform.position,boar.transform.position) < aggroRange)
    {
        attackDirection = new Vector2(player.position.x - boar.position.x, player.position.y - boar.position.y);
        aggroMagnitude = 0.075f / attackDirection.magnitude;
        if (aggroMagnitude > 0.075f)
        {
            aggroMagnitude = 0.075f;
        }
        attackDirection = Vector2.ClampMagnitude(attackDirection, aggroMagnitude);

        boar.Translate(attackDirection);
    }
}

}