Advertisement
EagleOwle

BotMove

Nov 16th, 2015
1,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BotMove : MonoBehaviour
  5. {
  6.     private BotState botState;
  7.     public Animator animator;
  8.     private Vector3 moveDirection;//Вектор для определения движения (направление движения используется)
  9.     public Vector3 aiDirection;
  10.     public float speedWalk = 30;//скорость движения вперед. Используется в функции Move
  11.     public float speedRotate = 100;
  12.     public Quaternion rotateDirection;
  13.     private CharacterController characterController;
  14.     public LayerMask moveCollisionLayerMask;
  15.  
  16.  
  17.     void Start()
  18.     {
  19.         botState = GetComponent<BotState>();
  20.         characterController = GetComponent<CharacterController>();
  21.         animator = GetComponent<Animator>();
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         if (botState.botStatus != BotStatus.Dead)
  27.         {
  28.             if (aiDirection != new Vector3(0f, 0f, 0f))
  29.             {
  30.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, rotateDirection, speedRotate * Time.deltaTime);
  31.             }
  32.         }
  33.     }
  34.  
  35.     void FixedUpdate()
  36.     {
  37.         if (botState.botStatus != BotStatus.Dead)
  38.         {
  39.             MoveBot();
  40.         }
  41.     }
  42.  
  43.     void MoveBot()
  44.     {
  45.         if (aiDirection != new Vector3(0f, 0f, 0f))
  46.         {
  47.             if (botState.startRepear != false) { botState.startRepear = false; animator.SetBool("repear", false); }
  48.             if (botState.startReload != false) { botState.startReload = false; animator.SetBool("repear", false); }
  49.         }
  50.        
  51.         if (botState.botStatus != BotStatus.PlayerControl)
  52.         {
  53.             CheckCollisionCapsul();
  54.         }
  55.  
  56.         moveDirection = aiDirection;
  57.         AnimateMove(moveDirection);
  58.         moveDirection = transform.TransformDirection(moveDirection);
  59.         moveDirection *= speedWalk;
  60.         moveDirection += Physics.gravity;
  61.         characterController.Move(moveDirection * Time.deltaTime);
  62.     }
  63.  
  64.     void CheckCollisionCapsul()
  65.     {
  66.         RaycastHit hitCollision;
  67.         Vector3  tmpPosition1 = transform.position + (Vector3.up * (characterController.radius + 1));
  68.         Vector3  tmpPosition2 = tmpPosition1 + (Vector3.up * characterController.height);
  69.         Vector3  tmpDirection = transform.TransformDirection(aiDirection);
  70.  
  71.         if (Physics.CapsuleCast(tmpPosition1, tmpPosition2, characterController.radius, tmpDirection, out hitCollision, 2f, moveCollisionLayerMask))
  72.         {
  73.             aiDirection = new Vector3(0f, 0f, 0f);
  74.             RandomPosition();
  75.  
  76.             tmpPosition1 = transform.position + (Vector3.up * (characterController.radius + 1));
  77.             tmpPosition2 = tmpPosition1 + (Vector3.up * characterController.height);
  78.             tmpDirection = transform.TransformDirection(aiDirection);
  79.  
  80.             if (Physics.CapsuleCast(tmpPosition1, tmpPosition2, characterController.radius, tmpDirection, out hitCollision, 2f, moveCollisionLayerMask))
  81.             {
  82.                 aiDirection = new Vector3(0f, 0f, 0f);
  83.                 //botAi.myTime += 3;
  84.                 //botAi.myRandomCheckTime += 3;
  85.             }
  86.         }  
  87.     }
  88.  
  89.     void RandomPosition()
  90.     {
  91.         int rnd = Random.Range(0, 8);
  92.  
  93.         if (rnd == 0)
  94.         {
  95.             aiDirection = new Vector3(0f, 0f, -0.1f);
  96.         }
  97.  
  98.         if (rnd == 1)
  99.         {
  100.             aiDirection = new Vector3(-0.1f, 0f, 0f);
  101.         }
  102.  
  103.         if (rnd == 2)
  104.         {
  105.             aiDirection = new Vector3(0.1f, 0f, 0f);
  106.         }
  107.  
  108.         if (rnd == 3)
  109.         {
  110.             aiDirection = new Vector3(0f, 0f, 0.1f);
  111.         }
  112.  
  113.         if (rnd == 4)
  114.         {
  115.             aiDirection = new Vector3(-0.1f, 0f, 0f);
  116.         }
  117.  
  118.         if (rnd == 5)
  119.         {
  120.             aiDirection = new Vector3(0.1f, 0f, 0f);
  121.         }
  122.  
  123.         if (rnd == 6)
  124.         {
  125.             aiDirection = new Vector3(0f, 0f, 0.1f);
  126.         }
  127.  
  128.         if (rnd == 7)
  129.         {
  130.             aiDirection = new Vector3(0.1f, 0f, 0f);
  131.         }
  132.  
  133.     }
  134.  
  135.     void AnimateMove(Vector3 direction)
  136.     {
  137.         if (direction == new Vector3(0f, 0f, 0.1f))
  138.         {
  139.             animator.SetInteger("walk", 1);
  140.         }
  141.  
  142.         if (direction == new Vector3(0f, 0f, -0.1f))
  143.         {
  144.             animator.SetInteger("walk", 3);
  145.         }
  146.  
  147.         if (direction == new Vector3(0.1f, 0f, 0f))
  148.         {
  149.             animator.SetInteger("walk", 2);
  150.         }
  151.  
  152.         if (direction == new Vector3(-0.1f, 0f, 0f))
  153.         {
  154.             animator.SetInteger("walk", 4);
  155.         }
  156.  
  157.         if (direction == new Vector3(0f, 0f, 0f))
  158.         {
  159.             animator.SetInteger("walk", 0);
  160.         }
  161.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement