- Home /
how to do a timer (TBS game)
using UnityEngine; using System.Collections;
public class Enemy_AI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public int maxDistance; public Transform starting;
public float attackTimer; public float coolDown;
public bool playAnimation ; //public Animation move; public Texture Bite; public Transform myTransform; void Awake (){ myTransform = transform; } // Use this for initialization void Start () { GameObject go = GameObject.FindGameObjectWithTag("Player_Slot_1"); target = go.transform; maxDistance = 20; attackTimer = 0; coolDown = 2.0f; moveSpeed = 0; } // Update is called once per frame void Update () { Debug.DrawLine(target.position, myTransform.position, Color.red); // Look at target myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed Time.deltaTime); if(Vector3.Distance(target.position, myTransform.position) > maxDistance){ // Move to target myTransform.position += myTransform.forward moveSpeed * Time.deltaTime; if (attackTimer > 0) attackTimer -= Time.deltaTime; if (attackTimer < 0) attackTimer = 0; if (attackTimer == 0) Attack(); if (attackTimer == 21) target = starting; animation.Play("idle"); if (attackTimer == 0) attackTimer = coolDown; if (moveSpeed >0) animation.Play("walk"); else animation.Play("idle");
} } void OnGUI(){ if (GUI.Button(new Rect(330, 130, 530, 50), Bite)) moveSpeed = 30; attackTimer = 0; coolDown = 2.0f; Attack(); }
private void Attack(){ float distance = Vector3.Distance(target.transform.position, transform.position); Vector3 dir = (target.transform.position - transform.position).normalized; float direction = Vector3.Dot(dir, transform.forward); //Debug.Log(distance); //Debug.Log(direction); if (distance < 25.5f){ if (direction > 0.0f){ PlayerHP ehp = (PlayerHP)target.GetComponent("PlayerHP"); animation.Play("attack"); ehp.AddjustCurrentHealth(-10); attackTimer = 22; }
} }
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613084934im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I move objects in one executed turn? 1 Answer
Tactics RPG / Turn Based 0 Answers
turn 180 spaceship 0 Answers
How to place an object on terrain without turning it? 0 Answers
Pathfinding in a Grid-Based RPG... Theorycrafting and solution. 1 Answer