Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy not rotating when inside range

$
0
0
Hello, I have had this problem for a while but I have not been able to find an answer. When the player gets within a certain distance, the enemy rotates and moves towards the player. When he within a closer distance, he stops so that he won't run into the player. When he is within the second distance, he stops rotating. I have an archer so when he stops rotating he won't be able to hit the player if he moves just inches from the side. This is what I have got: // vars var target : Vector3; public var dir : Vector3; private var dirFull : Vector3; public var mindist:float; public var maxdist:float; public var civilian:boolean; public var Wander:boolean; public var dist:float; public var wayPoint : Vector3; public var speed : float = 1; public var WanderDistance:float; public var pickPoint:boolean = true; public var CantMove:boolean; public var added:boolean; public var size:float; function Update() { var curY:float = transform.position.y; //Find The Player/Distance target = GameObject.FindGameObjectWithTag("Player").transform.position; dist = Vector3.Distance(target, transform.position); //Dont Want to shoot through walls. var hit : RaycastHit; //If it is between the maximum distance if(dist < maxdist){ //the minimum distance if(dist > mindist){ //direction to rotate dir = (target - transform.position).normalized; // check for forward raycast if (Physics.Raycast(transform.position, transform.forward, hit, 20)) { if (hit.transform != this.transform) { Debug.DrawLine (transform.position, hit.point, Color.white); dir += hit.normal * 20; // 20 is force to repel by } } // more raycasts var leftRay = transform.position + Vector3(-0.125, 0, 0); var rightRay = transform.position + Vector3(0.125, 0, 0); // check for leftRay raycast if (Physics.Raycast(leftRay, transform.forward, hit, 20)) { if (hit.transform != this.transform) { Debug.DrawLine (leftRay, hit.point, Color.red); dir += hit.normal * 20; // 20 is force to repel by } } // check for rightRay raycast if (Physics.Raycast(rightRay, transform.forward, hit, 20)) { if (hit.transform != this.transform) { Debug.DrawLine (rightRay, hit.point, Color.green); dir += hit.normal * 20; // 20 is force to repel by } } //Keep going forward transform.position += transform.forward * speed * Time.deltaTime; //Stay on the same Y position transform.position.y = curY; }//Ends Minimum Distance //Calculates rotation var rot; rot = Quaternion.LookRotation (dir); //Civilians run away if(civilian == true){ rot = Quaternion.Inverse(rot); } //Performs rotation transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime); //Ends Maximum Distance }else{ //Wandering When out of range PickPos(); transform.LookAt(wayPoint); transform.position += transform.forward * speed * Time.deltaTime; } } function PickPos() { if((transform.position - wayPoint).magnitude <= 3||pickPoint == true){ wayPoint= Random.insideUnitSphere *WanderDistance; wayPoint.y = transform.position.y; transform.LookAt(wayPoint); pickPoint = false; } } function OnCollisionEnter(hit:Collision){ transform.position += transform.forward * speed * -1 * Time.deltaTime; pickPoint = true; CantMove=true; } function OnCollisionExit(hit:Collision){ CantMove=false; }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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