Advertisement
LeeMace

Enemy Follow Player

Feb 10th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.50 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Enemy : MonoBehaviour
  6. {
  7.     public float speed = 3.0f;
  8.     private Rigidbody enemyRb;
  9.     private GameObject player;
  10.  
  11.  void Start()
  12.     {
  13.         enemyRb = GetComponent<Rigidbody>();
  14.         player = GameObject.Find("Player");
  15.     }
  16.  
  17.  void Update()
  18.     {
  19.         Vector3 lookDirection = (player.transform.position - transform.position).normalized;
  20.         enemyRb.AddForce(lookDirection * speed);
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement