Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyScript : MonoBehaviour
- {
- public PlayerScript ps;
- public Rigidbody rb;
- public float enemySpeed = 3f;
- public float enemyNoticeDistance = 4f;
- // Start is called before the first frame update
- private void Start()
- {
- }
- // Update is called once per frame
- private void Update()
- {
- Vector3 differenceFromPlayer3D = ps.transform.position - transform.position;
- Vector2 differenceFromPlayer2D = new Vector2(differenceFromPlayer3D.x, differenceFromPlayer3D.z);
- if (differenceFromPlayer2D.magnitude < enemyNoticeDistance)
- {
- Vector2 directionToPlayer2D = differenceFromPlayer2D.normalized * enemySpeed;
- Vector3 directionToPlayer3D = new Vector3(directionToPlayer2D.x, rb.velocity.y, directionToPlayer2D.y);
- rb.velocity = directionToPlayer3D;
- }
- else
- {
- rb.velocity = new Vector3(0, rb.velocity.y, 0);
- }
- }
- private void OnCollisionEnter(Collision other)
- {
- PlayerScript ps = other.collider.GetComponent<PlayerScript>();
- if (ps != null)
- {
- ps.health--;
- Object.Destroy(this.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement