Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

How to make the enemy hit me if is in range and if not then proceeding back to his waypoint

$
0
0
Hi guys i wonder how to make the enemy hit you when he saw you and if is out of the range then it will proceed back to his waypoint. I have actually done the search target script as well as the waypoints from the web, but both is in different file when i put them together it cant see to work, any1 know why? I have separate it back to two file. Waypoint.cs using UnityEngine; using System.Collections; public class SeekSteer : MonoBehaviour { public Transform[] waypoints; public float waypointRadius = 1.5f; public float damping = 0.1f; public bool loop = false; public float speed = 2.0f; public bool faceHeading = true; private Vector3 currentHeading,targetHeading; private int targetwaypoint; private Transform xform; private bool useRigidbody; private Rigidbody rigidmember; // Use this for initialization protected void Start () { xform = transform; currentHeading = xform.forward; if(waypoints.Length<=0) { Debug.Log("No waypoints on "+name); enabled = false; } targetwaypoint = 0; if(rigidbody!=null) { useRigidbody = true; rigidmember = rigidbody; } else { useRigidbody = false; } } // calculates a new heading protected void FixedUpdate () { targetHeading = waypoints[targetwaypoint].position - xform.position; currentHeading = Vector3.Lerp(currentHeading,targetHeading,damping*Time.deltaTime); } // moves us along current heading protected void Update() { if(useRigidbody) rigidmember.velocity = currentHeading * speed; else xform.position +=currentHeading * Time.deltaTime * speed; if(faceHeading) xform.LookAt(xform.position+currentHeading); if(Vector3.Distance(xform.position,waypoints[targetwaypoint].position)<=waypointRadius) { targetwaypoint++; if(targetwaypoint>=waypoints.Length) { targetwaypoint = 0; if(!loop) enabled = false; } } } // draws red line from waypoint to waypoint public void OnDrawGizmos() { Gizmos.color = Color.red; if(waypoints==null) return; for(int i=0;i< waypoints.Length;i++) { Vector3 pos = waypoints[i].position; if(i>0) { Vector3 prev = waypoints[i-1].position; Gizmos.DrawLine(prev,pos); } } } } enemy.cs using UnityEngine; using System.Collections; public class Enemy2 : MonoBehaviour { public bullettower1 bulletPrefab = null; public Transform target; public int moveSpeed; public int rotationSpeed; float range = 10.0f; float range2 = 15.0f; float range3 = 4.0f; float nextFire = 0; float fireRate = 1.5f; float stop = 3.0f; Transform myTransform; [SerializeField] public Transform hand; void Awake(){ myTransform = transform; } // Use this for initialization void Start () { GameObject go = GameObject.FindGameObjectWithTag("Player"); animation.CrossFade("idle"); target = go.transform; } // Update is called once per frame void Update () { float distance = Vector3.Distance(myTransform.position, target.position); if (distance <= range2 && distance >= range){ myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); } else if(distance <= range && distance > stop){ //move towards the player animation.CrossFade("run"); myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; } else if (distance <=stop) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); } else if (distance >= range) { animation.CrossFade("idle"); } if (distance <= range3) { animation.CrossFade("Attack"); if (Time.time > nextFire) { nextFire = Time.time + fireRate; GameObject g = (GameObject)Instantiate(bulletPrefab.gameObject, hand.position, Quaternion.identity); bullettower1 b = g.GetComponent(); b.setDestination(target.transform); } } } }

Viewing all articles
Browse latest Browse all 171066

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>