Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.AI;
- public class Robot : MonoBehaviour
- {
- public float rayLength = 20f;
- GameObject target;
- float startRotation;
- float t = 0;
- public float maxTurnAngle = 45.0f;
- GameObject valonlahde;
- public AudioClip alert;
- GameObject player;
- // Use this for initialization
- void Start()
- {
- startRotation = transform.rotation.eulerAngles.y;
- valonlahde = GameObject.Find("Hehku");
- player = GameObject.FindWithTag("Player");
- }
- // Update is called once per frame
- void Update()
- {
- if (target == null)
- {
- Turn();
- DetectPlayer();
- }
- else
- {
- Move();
- }
- }
- void Move()
- {
- NavMeshAgent agent = GetComponent<NavMeshAgent>();
- agent.SetDestination(target.transform.position);
- }
- void DetectPlayer()
- {
- Debug.DrawRay(transform.position, transform.forward * rayLength, Color.red);
- RaycastHit hitInfo;
- if (Physics.Raycast(transform.position, transform.forward, out hitInfo, rayLength, 1))
- {
- GameObject hitGameObject = hitInfo.collider.gameObject;
- if (hitGameObject.tag == "Player")
- {
- target = hitGameObject;
- valonlahde.GetComponent<Light>().color = Color.red;
- AudioSource.PlayClipAtPoint(alert, player.transform.position);
- }
- }
- }
- void OnCollisionEnter(Collision collision)
- {
- if (collision.gameObject.tag == "Player")
- {
- GameObject.Find("Level").SendMessage("Kill");
- }
- }
- void Turn()
- {
- t += Time.deltaTime;
- transform.rotation = Quaternion.Euler(0, startRotation + Mathf.Sin(t) * maxTurnAngle, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement