Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TestAi : MonoBehaviour
  6. {
  7.     float speed = 3f;
  8.     public Transform Player;
  9.  
  10.     float maxHealth = 10f;
  11.     float health;
  12.     public Collider body;
  13.     float playerDamage;
  14.  
  15.  
  16.     void Start()
  17.     {
  18.         health = maxHealth;
  19.         playerDamage = KnightCntrl.damage;
  20.     }
  21.  
  22.     // Update is called once per frame
  23.     void Update()
  24.     {
  25.         MoveTowardsPlayer();
  26.  
  27.         if(health <= 0)
  28.         {
  29.             Destroy(this.gameObject);
  30.         }
  31.     }
  32.  
  33.     void MoveTowardsPlayer()
  34.     {
  35.         if (Vector3.Distance(Player.position, transform.position) < 10)
  36.         {
  37.             Vector3 dir = Player.position - transform.position;
  38.             dir.y = 0;
  39.  
  40.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), 0.1f);
  41.  
  42.             if (dir.magnitude < 10 && dir.magnitude > 1)
  43.             {
  44.                 transform.Translate(0f, 0f, speed * Time.deltaTime);
  45.             }
  46.  
  47.         }
  48.     }
  49.  
  50.     private void OnTriggerEnter(Collider body)
  51.     {
  52.         if (body.CompareTag("Sword")) { health -= playerDamage; }
  53.         Debug.Log(health);
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement