Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AdvancedGamingBR · Apr 12, 2014 at 06:17 PM · aiwaypointzombie

Way Point Script Change Group

changing group waypoint during the game? I got but it keeps following the same group, but if I call the function again the speed increases.

 public var objectWithAnims : Transform;//the object with the Animation component automatically created by the character mesh's import settings
 private var minimumRunSpeed = 3.0;
 public var walkAnimSpeed = 1.0;
 public var runAnimSpeed = 1.0;
 var speed = 6.0;//movement speed of the NPC
 private var speedAmt = 1.0;
 var pushPower = 5.0;//physics force to apply to rigidbodies blocking NPC path
 var rotationSpeed = 5.0;
 var targetPlayer = true;//to determine if NPC should ignore player
 var shootRange = 15.0;//minimum range to target for attack
 var attackRange = 30.0;//range that NPC will start chasing target until they are within shootRange
 @HideInInspector
 var attackRangeAmt = 30.0;//increased by character damage script if NPC is damaged by player
 var sneakRangeMod : float = 0.4f;//reduce NPC's attack range by sneakRangeMod amount when player is sneaking
 private var shootAngle = 2.0;
 var dontComeCloserRange = 5.0;
 var delayShootTime = 0.35;
 private var delayShootTimeRand = 0.0;
 private var pickNextWaypointDistance = 2.0;
 @HideInInspector
 var target : Transform;
 @HideInInspector
 var playerObj : GameObject;
 private var myTransform : Transform;
 private var initMove : float;
 var doPatrol = true;
 var walkOnPatrol = true;
 public var myWaypointGroup : int;//waypoint group number that this NPC should patrol
 var searchMask : LayerMask = 0;//only layers to include in target search (for efficiency)
 var eyeHeight = 0.4;//height of rayCast starting point/origin which detects player (can be raised if NPC origin is at their feet)
 private var countBackwards : boolean = false;
 public var randomSpawnChance : float = 1.0f;//
 private var lastShot = -10.0;
 
 // Make sure there is always a character controller
 @script RequireComponent (CharacterController)
 
 function Start () {
 
     Mathf.Clamp01(randomSpawnChance);
 
     // Activate the npc based on randomSpawnChance
     if(Random.value > randomSpawnChance){
         Destroy(transform.gameObject);
     }else{
     
         //if there is no objectWithAnims defined, use the Animation Component attached to this game object
         if(objectWithAnims == null){objectWithAnims = transform;}
 
         // Set all animations to loop
         objectWithAnims.animation.wrapMode = WrapMode.Loop;
     
         // Except our action animations, Dont loop those
         objectWithAnims.animation["shoot"].wrapMode = WrapMode.Once;
         // Put idle and run in a lower layer. They will only animate if our action animations are not playing
         objectWithAnims.animation["idle"].layer = -1;
         objectWithAnims.animation["walk"].layer = -1;
         objectWithAnims.animation["run"].layer = -1;
         
         objectWithAnims.animation["walk"].speed = walkAnimSpeed;
         objectWithAnims.animation["run"].speed = runAnimSpeed;
         
         objectWithAnims.animation.Stop();
     
         //initialize AI vars
         playerObj = Camera.main.transform.GetComponent("CameraKick").playerObj;
         attackRangeAmt = attackRange;
         initMove = Time.time;
         objectWithAnims.animation.CrossFade("idle", 0.3);
         // Auto setup player as target through tags
         if(target == null && GameObject.FindWithTag("Player") && targetPlayer){
             target = GameObject.FindWithTag("Player").transform;
         }
         if(doPatrol){
             Patrol();
         }else{
             StandWatch();
         }
         
         if(!targetPlayer){
             //ignore collisions with player if NPC is not targeting player to prevent physics oddities
             transform.gameObject.layer = 9;
         }
     }
 }
 
 function Awake () {
     myTransform = transform;
 }
 
 function StandWatch () {
     var controller : CharacterController = GetComponent(CharacterController);    
     while (true) {
     
         //play idle animation
         objectWithAnims.animation.CrossFade("idle", 0.3);
         
         //if NPC spawns in the air, move their character controller to the ground
         if(!controller.isGrounded){ 
             var down = myTransform.TransformDirection(-Vector3.up);
             controller.SimpleMove(down);
         }else{        
             if (CanSeeTarget()){
                 yield StartCoroutine("AttackPlayer");
             }
         }
         
         yield new WaitForFixedUpdate ();//could wait for longer interval than fixed update for efficiency
     }
 }
 
 function Patrol () {
     //find the next waypoint and pass our myWaypointGroup number to AutoWayPoint script
     var curWayPoint = AutoWayPoint.FindClosest(myTransform.position, myWaypointGroup);
     var controller : CharacterController = GetComponent(CharacterController);
     if(curWayPoint){//patrol if NPC has a current waypoint, otherwise stand watch
         while (true) {
             var waypointPosition = curWayPoint.transform.position;
             // Are we close to a waypoint? -> pick the next one!
             if(Vector3.Distance(waypointPosition, myTransform.position) < pickNextWaypointDistance){
                 curWayPoint = PickNextWaypoint (curWayPoint);
             }
             
             //if NPC spawns in the air, move their character controller to the ground
             if(!controller.isGrounded){ 
                 var down = myTransform.TransformDirection(-Vector3.up);
                 controller.SimpleMove(down);
             }else{    
                 // Attack the player and wait until
                 // - player is killed
                 // - player is out of sight    
                 if(target){    
                     if(CanSeeTarget()){
                         yield StartCoroutine("AttackPlayer");
                     }
                 }
             }
             //determine if NPC should walk or run on patrol
             if(walkOnPatrol){speedAmt = 1.0f;}else{speedAmt = speed;}
             // Move towards our target
             MoveTowards(waypointPosition);
             
             yield new WaitForFixedUpdate ();
         }
     }else{
         StandWatch();
         return;
     }
     }
 
 
 function CanSeeTarget () : boolean {
     var FPSWalker = playerObj.GetComponent("FPSRigidBodyWalker");
     if(FPSWalker.crouched){
         attackRangeAmt = attackRange * sneakRangeMod;//reduce NPC's attack range by sneakRangeMod amount when player is sneaking
     }else{
         attackRangeAmt = attackRange;
     }
     if(Vector3.Distance(myTransform.position, target.position) > attackRangeAmt){
         return false;
     }
     var hit : RaycastHit;
     if(Physics.Linecast (myTransform.position + myTransform.up * (1.0 + eyeHeight), target.position, hit, searchMask)){
         return hit.transform == target;
     }
     return false;
 }
 
 function Shoot () {
     // Start shoot animation
     objectWithAnims.animation.CrossFade("shoot", 0.3);
     speedAmt = 0.0f;
     //SendMessage("SetSpeed", 0.0);
     SetSpeed(0.0f);
     // Wait until half the animation has played
     yield WaitForSeconds(delayShootTime);
     // Fire gun
     BroadcastMessage("Fire");
     // Wait for the rest of the animation to finish
     yield WaitForSeconds(objectWithAnims.animation["shoot"].length - delayShootTime + Random.Range(0.0f, 0.75f));
 }
 
 function AttackPlayer () {
     var lastVisiblePlayerPosition = target.position;
     while (true) {
         if(CanSeeTarget ()){
             // Target is dead - stop hunting
             if(target == null){
                 speedAmt = 1.0f;
                 return;
             }
             // Target is too far away - give up    
             var distance = Vector3.Distance(myTransform.position, target.position);
             if(distance > attackRangeAmt){
                 speedAmt = 1.0f;
                 return;
             }
             speedAmt = speed;
             lastVisiblePlayerPosition = target.position;
             if(distance > dontComeCloserRange){
                 MoveTowards (lastVisiblePlayerPosition);
             }else{
                 RotateTowards(lastVisiblePlayerPosition);
             }
             var forward = myTransform.TransformDirection(Vector3.forward);
             var targetDirection = lastVisiblePlayerPosition - myTransform.position;
             targetDirection.y = 0;
 
             var angle = Vector3.Angle(targetDirection, forward);
 
             // Start shooting if close and player is in sight
             if(distance < shootRange && angle < shootAngle){
                 yield StartCoroutine("Shoot");
             }
         }else{
             speedAmt = speed;
             yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
             // Player not visible anymore - stop attacking
             if (!CanSeeTarget ()){
                 speedAmt = 1.0f;
                 return;
             }
         }
 
         yield;//dont wait any frames for smooth NPC movement while attacking player
     }
 }
 
 function SearchPlayer (position : Vector3) {
     // Run towards the player but after 3 seconds timeout and go back to Patroling
     var timeout = 3.0;
     while(timeout > 0.0){
         MoveTowards(position);
 
         // We found the player
         if(CanSeeTarget()){
             return;
         }
         timeout -= Time.deltaTime;
 
         yield;//dont wait any frames for smooth NPC movement while searching for player
     }
 }
 
 function RotateTowards (position : Vector3) {
     //SendMessage("SetSpeed", 0.0);
     SetSpeed(0.0f);
     
     var direction = position - myTransform.position;
     direction.y = 0;
     if(direction.magnitude < 0.1){
         return;
     }
     // Rotate towards the target
     myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime * 100);
     myTransform.eulerAngles = Vector3(0, myTransform.eulerAngles.y, 0);
 }
 
 function MoveTowards (position : Vector3) {
     var direction = position - myTransform.position;
     direction.y = 0;
     if(direction.magnitude < 0.5){
         SetSpeed(0.0f);
         return;
     }
     
     // Rotate towards the target
     myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
     myTransform.eulerAngles = Vector3(0, myTransform.eulerAngles.y, 0);
     // Modify speed so we slow down when we are not facing the target
     var forward = myTransform.TransformDirection(Vector3.forward);
     var speedModifier = Vector3.Dot(forward, direction.normalized);
     speedModifier = Mathf.Clamp01(speedModifier);
     // Move the character
     direction = forward * speedAmt * speedModifier;
     GetComponent (CharacterController).SimpleMove(direction);
 
     SetSpeed(speedAmt * speedModifier);
     
 }
 
 function PickNextWaypoint (currentWaypoint : AutoWayPoint) {
 
     var best = currentWaypoint;
 
     for(var cur : AutoWayPoint in currentWaypoint.connected){
 
         if(!countBackwards){
             if(currentWaypoint.waypointNumber != cur.connected.length){
                 if(currentWaypoint.waypointNumber + 1 == cur.waypointNumber){
                     best = cur;
                     break;
                 }
             }else if(currentWaypoint.waypointNumber == cur.connected.length){
                 if(currentWaypoint.waypointNumber -1 == cur.waypointNumber){
                     best = cur;
                     countBackwards = true;
                     break;
                 }
             }
         }else{
             if(currentWaypoint.waypointNumber != 1){
                 if(currentWaypoint.waypointNumber - 1 == cur.waypointNumber){
                     best = cur;
                     break;
                 }
             }else if(currentWaypoint.waypointNumber == 1){
                 if(currentWaypoint.waypointNumber + 1 == cur.waypointNumber){
                     best = cur;
                     countBackwards = false;
                     break;
                 }
             }
         
         }
         
         
     }
     
     return best;
 }
 
 //allow the NPCs to push rigidbodies in their path
 function OnControllerColliderHit (hit : ControllerColliderHit) {
     var body : Rigidbody = hit.collider.attachedRigidbody;
     // no rigidbody
     if (body == null || body.isKinematic || body.gameObject.tag == "Player")
         return;
         
     // We dont want to push objects below us
     if (hit.moveDirection.y < -0.3) 
         return;
     
     // Calculate push direction from move direction, 
     // we only push objects to the sides never up and down
     var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
     // If you know how fast your character is trying to move,
     // then you can also multiply the push velocity by that.
     
     // Apply the push
     body.velocity = pushDir * pushPower;
 }
 
 function SetSpeed (speed : float) {
     if (speed > minimumRunSpeed){
         objectWithAnims.animation.CrossFade("run");
     }else{
         if(speed > 0){
             objectWithAnims.animation.CrossFade("walk");
         }else{
             objectWithAnims.animation.CrossFade("idle");
         }
     }
 }



Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Apr 12, 2014 at 09:40 PM 0
Share

Posting so much code with little direction makes it unlikely you will get an answer. Isolating the problem to a particular section of the code and modifying your question to indicate the section will greatly increase the chance of an answer.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Running and Follow at Certain Distance 0 Answers

Check whether car has passed a point in world space 1 Answer

[Zombie AI] Enemy grows and pushes me when coming towards me? 1 Answer

ZOMBIE AI SCRIPT 1 Answer

how to create waypoints for a car to follow? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges