Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class EnemyMove : MonoBehaviour {
- private int _direction = -1;
- private CharacterController _characterController;
- public float Speed = 5.0f;
- //Graity
- public float Gravity = 9.81f;
- public bool StickToPlatform = false;
- public bool CanPushRubber = false;
- public bool CanPushWood = false;
- public bool CanPushMetal = false;
- private float _pushPower = 2.0f; //Please leave this unchanged, the code handles this for you
- public bool FollowPlayer = false;
- public float FollowPlayerRange = 10.0f;
- private float JumpDepleter = 80.0f; //the amount to degrade when doing regular jump
- public bool CanJump = false;
- public float JumpSpeed = 30.0f; //The base jumping speed
- private float _currentJumpSpeed = 0.0f; //the current jump speed, can degrade
- private GameObject _player;
- private bool _isFollowing = false;
- private float _timeStart = 0.0f;
- private bool _timerStarted = false;
- // Use this for initialization
- void Start () {
- _characterController = this.GetComponent<CharacterController>();
- _player = GameObject.Find("Player");
- }
- // Update is called once per frame
- void Update () {
- //if (_modeSwitch.GetGameMode() == GameMode.Platform) //no Movement in Editor mode
- //{
- if (FollowPlayer)
- {
- Vector3 _distanceToPlayer = transform.position - _player.transform.position;
- if (_distanceToPlayer.magnitude < FollowPlayerRange)
- {
- int tempDir = (int)Mathf.Round(-_distanceToPlayer.normalized.x);
- if (tempDir != 0)
- {
- _direction = tempDir;
- _isFollowing = true;
- }
- }
- else
- _isFollowing = false;
- }
- if (StickToPlatform)
- {
- if (!_isFollowing)
- if (!(Physics.Raycast(transform.position, (transform.forward * _direction + new Vector3(0, -0.6f, 0) * 2f), 2f)))
- _direction *= -1;
- }
- RaycastHit[] hit = Physics.RaycastAll(transform.position, (transform.forward * _direction), 1f);
- Debug.Log(hit.Length);
- if (hit.Length > 0)
- {
- if (hit[0].transform.tag != "Player")
- {
- if (_characterController.isGrounded && CanJump)
- {
- if (Physics.Raycast(transform.position + new Vector3(0, JumpSpeed, 0), (transform.forward * _direction * 2f), 2f))
- _currentJumpSpeed = JumpSpeed;
- }
- else
- {
- bool changeDirection = true;
- if (hit[0].transform.tag == "Rubber" && CanPushRubber)
- changeDirection = false;
- else if (hit[0].transform.tag == "Wood" && CanPushWood)
- changeDirection = false;
- else if (hit[0].transform.tag == "Metal" && CanPushMetal)
- changeDirection = false;
- if (changeDirection)
- _direction *= -1;
- }
- }
- }
- Debug.Log(_direction);
- _characterController.Move(new Vector3(Speed * _direction, -Gravity, 0)* Time.deltaTime);
- //Debug infromation for the raycasts
- //Debug.DrawRay(transform.position, (transform.forward * m_Direction * 1f));
- //Debug.DrawRay(transform.position, (transform.forward * m_Direction + new Vector3(0, -0.6f, 0)* 1.4f));
- }
- public void ChangeDirection()
- {
- _direction *= -1;
- }
- void OnControllerColliderHit(ControllerColliderHit hit)
- {
- Rigidbody body = hit.collider.attachedRigidbody;
- if (body == null || body.isKinematic)
- return;
- if (hit.moveDirection.y < -0.3f)
- return;
- if (_characterController.velocity.x * _direction < 0.9f)
- _timerStarted = true;
- else
- {
- _timerStarted = false;
- _timeStart = 0.0f;
- }
- if (_timerStarted)
- _timeStart += Time.deltaTime;
- if (_timeStart >= 0.28f)
- {
- ChangeDirection();
- _timerStarted = false;
- _timeStart = 0.0f;
- return;
- }
- Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
- body.velocity = pushDir * _pushPower;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement