Advertisement
ToastyStoemp

Enemy

Jan 24th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyMove : MonoBehaviour {
  5.  
  6.     private int _direction = -1;
  7.     private CharacterController _characterController;
  8.  
  9.     public float Speed = 5.0f;
  10.  
  11.     //Graity
  12.     public float Gravity = 9.81f;
  13.  
  14.     public bool StickToPlatform = false;
  15.     public bool CanPushRubber = false;
  16.     public bool CanPushWood = false;
  17.     public bool CanPushMetal = false;
  18.  
  19.     private float _pushPower = 2.0f; //Please leave this unchanged, the code handles this for you
  20.  
  21.     public bool FollowPlayer = false;
  22.     public float FollowPlayerRange = 10.0f;
  23.     private float JumpDepleter = 80.0f; //the amount to degrade when doing regular jump
  24.  
  25.     public bool CanJump = false;
  26.  
  27.     public float JumpSpeed = 30.0f; //The base jumping speed
  28.     private float _currentJumpSpeed = 0.0f; //the current jump speed, can degrade
  29.  
  30.     private GameObject _player;
  31.     private bool _isFollowing = false;
  32.  
  33.     private float _timeStart = 0.0f;
  34.     private bool _timerStarted = false;
  35.    
  36.     // Use this for initialization
  37.     void Start () {
  38.         _characterController = this.GetComponent<CharacterController>();
  39.         _player = GameObject.Find("Player");
  40.     }
  41.    
  42.     // Update is called once per frame
  43.     void Update () {
  44.         //if (_modeSwitch.GetGameMode() == GameMode.Platform) //no Movement in Editor mode
  45.         //{
  46.  
  47.         if (FollowPlayer)
  48.         {
  49.             Vector3 _distanceToPlayer = transform.position - _player.transform.position;
  50.             if (_distanceToPlayer.magnitude < FollowPlayerRange)
  51.             {
  52.  
  53.                 int tempDir = (int)Mathf.Round(-_distanceToPlayer.normalized.x);
  54.                 if (tempDir != 0)
  55.                 {
  56.                     _direction = tempDir;
  57.                     _isFollowing = true;
  58.                 }
  59.             }
  60.             else
  61.                 _isFollowing = false;
  62.         }
  63.  
  64.         if (StickToPlatform)
  65.         {
  66.             if (!_isFollowing)
  67.                 if (!(Physics.Raycast(transform.position, (transform.forward * _direction + new Vector3(0, -0.6f, 0) * 2f), 2f)))
  68.                     _direction *= -1;
  69.         }
  70.         RaycastHit[] hit = Physics.RaycastAll(transform.position, (transform.forward * _direction), 1f);
  71.         Debug.Log(hit.Length);
  72.         if (hit.Length > 0)
  73.         {
  74.             if (hit[0].transform.tag != "Player")
  75.             {
  76.                 if (_characterController.isGrounded && CanJump)
  77.                 {
  78.                     if (Physics.Raycast(transform.position + new Vector3(0, JumpSpeed, 0), (transform.forward * _direction * 2f), 2f))
  79.                         _currentJumpSpeed = JumpSpeed;
  80.                 }
  81.                 else
  82.                 {
  83.                     bool changeDirection = true;
  84.  
  85.                     if (hit[0].transform.tag == "Rubber" && CanPushRubber)
  86.                         changeDirection = false;
  87.                     else if (hit[0].transform.tag == "Wood" && CanPushWood)
  88.                         changeDirection = false;
  89.                     else if (hit[0].transform.tag == "Metal" && CanPushMetal)
  90.                         changeDirection = false;
  91.  
  92.                     if (changeDirection)
  93.                         _direction *= -1;
  94.                 }
  95.             }
  96.         }
  97.  
  98.         Debug.Log(_direction);
  99.         _characterController.Move(new Vector3(Speed * _direction, -Gravity, 0)* Time.deltaTime);
  100.    
  101.         //Debug infromation for the raycasts
  102.         //Debug.DrawRay(transform.position, (transform.forward * m_Direction * 1f));
  103.         //Debug.DrawRay(transform.position, (transform.forward * m_Direction + new Vector3(0, -0.6f, 0)* 1.4f));
  104.     }
  105.  
  106.     public void ChangeDirection()
  107.     {
  108.         _direction *= -1;
  109.     }
  110.  
  111.    
  112.     void OnControllerColliderHit(ControllerColliderHit hit)
  113.     {
  114.         Rigidbody body = hit.collider.attachedRigidbody;
  115.         if (body == null || body.isKinematic)
  116.             return;
  117.  
  118.         if (hit.moveDirection.y < -0.3f)
  119.             return;
  120.  
  121.  
  122.         if (_characterController.velocity.x * _direction < 0.9f)
  123.             _timerStarted = true;
  124.         else
  125.         {
  126.             _timerStarted = false;
  127.             _timeStart = 0.0f;
  128.         }
  129.  
  130.         if (_timerStarted)
  131.             _timeStart += Time.deltaTime;
  132.  
  133.         if (_timeStart >= 0.28f)
  134.         {
  135.             ChangeDirection();
  136.             _timerStarted = false;
  137.             _timeStart = 0.0f;
  138.             return;
  139.         }
  140.  
  141.         Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
  142.         body.velocity = pushDir * _pushPower;
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement