dronkowitz

EggPawn.cs

May 12th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EggPawn : MonoBehaviour
  6. {
  7.     public Transform player;
  8.     private Health myHealth;
  9.     // Start is called before the first frame update
  10.     void Start()
  11.     {
  12.         player = GameObject.FindWithTag("Player").transform;
  13.         myHealth = GetComponent<Health>();
  14.     }
  15.  
  16.     // Update is called once per frame
  17.     void Update()
  18.     {
  19.         if (myHealth.dead) return;
  20.         if (Vector3.Distance(transform.position, player.position) < 10)
  21.         {
  22.             LookAt(player);
  23.             if (Vector3.Distance(transform.position, player.position) < 3)
  24.             {
  25.                 GetComponent<Animator>().SetTrigger("Attack");
  26.             }
  27.         }
  28.     }
  29.  
  30.     public void DamagePlayer()
  31.     {
  32.         if (Vector3.Distance(transform.position, player.position) < 3)
  33.         {
  34.             Debug.Log("Damage Player");
  35.         }
  36.     }
  37.  
  38.     public void LookAt(Transform target)
  39.     {
  40.         Vector3 targetPosition = target.position;
  41.         targetPosition.y = transform.position.y;
  42.         Vector3 direction = targetPosition - transform.position;
  43.         direction.Normalize();
  44.         transform.rotation = Quaternion.LookRotation(direction);
  45.     }
  46. }
  47.  
Add Comment
Please, Sign In to add comment