Advertisement
jtwolf

Random Walk RAIN AI - Unity 3D

Nov 3rd, 2013
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using RAIN.Core;
  5. using RAIN.Action;
  6.  
  7. [RAINAction]
  8. public class RandomWalk : RAINAction
  9. {
  10.    
  11.     public Vector3 directionVector;
  12.    
  13.    
  14.     //GameObject that is going to wander and contain AI
  15.     float distanceToWander;
  16.     int minDistanceToWander;
  17.     int maxDistanceToWander;
  18.     int maxTurnAngle;
  19.    
  20.    
  21.     public RandomWalk()
  22.     {
  23.         actionName = "RandomWalk";
  24.     }
  25.  
  26.     public override void Start(AI ai)
  27.     {
  28.            
  29.         base.Start(ai);
  30.                
  31.         //Load Parameters for Wander
  32.         maxTurnAngle = ai.WorkingMemory.GetItem<int>("maxTurnAngle");
  33.         minDistanceToWander = ai.WorkingMemory.GetItem<int>("minDistanceToWander");
  34.         maxDistanceToWander = ai.WorkingMemory.GetItem<int>("maxDistanceToWander");
  35.        
  36.         //Calculate Distance to Wander
  37.         distanceToWander = Random.Range(minDistanceToWander,maxDistanceToWander);
  38.                
  39.         //Set Random Direction
  40.         Vector2 newVector = Random.insideUnitCircle;
  41.         directionVector.x = newVector.x * distanceToWander;
  42.         directionVector.z = newVector.y * distanceToWander;
  43.         directionVector.y = 0;
  44.        
  45.         float angle = Vector3.Angle(ai.Body.transform.forward, directionVector);
  46.        
  47.         //Check if angle is larger than Max Turning Angle
  48.         while (angle > maxTurnAngle){
  49.        
  50.             //Set Random Direction
  51.             newVector = Random.insideUnitCircle;
  52.             directionVector.x = newVector.x * distanceToWander;
  53.             directionVector.z = newVector.y * distanceToWander;
  54.             directionVector.y = 0;
  55.            
  56.             angle = Vector3.Angle(ai.Body.transform.forward, directionVector);
  57.            
  58.         }
  59.        
  60.         //Random Chance for bigger angle of rotation - Helps to break out object from edge of nav mesh
  61.         if( (Random.Range(0,10)) < 2){
  62.        
  63.             //Set Random Direction
  64.             newVector = Random.insideUnitCircle;
  65.             directionVector.x = newVector.x * distanceToWander;
  66.             directionVector.z = newVector.y * distanceToWander;
  67.             directionVector.y = 0;
  68.            
  69.             angle = Vector3.Angle(ai.Body.transform.forward, directionVector);
  70.         }
  71.                                
  72.         //Set Position to Move to relative to Current Position
  73.         directionVector = ai.Body.transform.position + directionVector;
  74.         directionVector.y = Terrain.activeTerrain.SampleHeight(directionVector);
  75.        
  76.         //Set location into working memory for Move action in BT
  77.         ai.WorkingMemory.SetItem<Vector3>("directionVector", directionVector);
  78.                
  79.     }
  80.  
  81.     public override ActionResult Execute(AI ai)
  82.     {  
  83.    
  84.         return ActionResult.SUCCESS;
  85.        
  86.     }
  87.    
  88.     public override void Stop(AI ai)
  89.     {
  90.         base.Stop(ai);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement