Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class EnemySold : MonoBehaviour {
- CharacterController cc;
- public float gravity;
- private Vector3 moveDirection = Vector3.zero;
- public float moveSpeed;
- public float jumpForce;
- public float shootRate;
- float rate;
- public GameObject actor;
- public CapsuleCollider collisionCapsule;
- public GameObject bullet;
- public Transform bulletSpawnPoint;
- public Transform rayPoint;
- int dir;
- public bool playerInSight;
- //
- void Start ()
- {
- if(cc == null)
- {
- cc = this.gameObject.AddComponent<CharacterController>();
- cc.height = 1;
- cc.center = new Vector3(0, 0.5f, 0);
- dir = 0;
- actor.transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
- playerInSight = false;
- }
- }
- void FixedUpdate()
- {
- RaycastHit hit;
- Debug.DrawRay(rayPoint.position, rayPoint.right, Color.green);
- if (Physics.Raycast(rayPoint.position, rayPoint.right, out hit, 10.0f)){
- if(hit.transform.tag == "wall" && hit.distance < 1)
- {
- // Looking left turn to Right
- if(dir == 1)
- {
- dir = 0;
- actor.transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
- }
- // Looking right turn to Left
- if(dir == 0)
- {
- dir = 1;
- actor.transform.rotation = Quaternion.Euler(new Vector3(0,180,0));
- }
- }
- if(hit.transform.tag == "Player")
- {
- playerInSight = true;
- }
- }
- else
- {
- playerInSight = false;
- }
- }
- void Update ()
- {
- // shoot!
- if(playerInSight)
- {
- rate -= 1 * Time.deltaTime;
- }
- // move!
- if(!playerInSight && cc.isGrounded)
- {
- if(dir == 0)
- {
- moveDirection = new Vector3(moveSpeed, 0, 0);
- }
- if(dir == 1)
- {
- moveDirection = new Vector3(-moveSpeed, 0, 0);
- }
- }
- if(rate <= 0)
- {
- Instantiate(bullet, bulletSpawnPoint.transform.position , bulletSpawnPoint.transform.rotation);
- rate = shootRate;
- }
- // gravity!
- moveDirection.y -= gravity * Time.deltaTime;
- cc.Move(moveDirection * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement