Unity Enemy AI Movement - codebugfree

Unity Enemy ai movent is the import for the vide game. If you face the programming error in Enemy AI. I will give you enemy ai script. It is the simple script. I will provide the simple script that script provide you enemy provide the movement. There is also many other funcationality provided your player. I will explain the all funcationality of enemy.

Unity enemy AI Script

Funcationality of enemy 

* enemy follow the player

* Enemy is also follow the player with see the player. Enemy angle is and player angle is the       same.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
    public Transform enemy;
    public Animator enemyrunAnimation;
    private Rigidbody enemyRb;
    public float enemySpeed = 0.002f;
    public GameObject playerTransform;
    public Score score;
    // Start is called before the first frame update
    void Start()
        enemyRb = GetComponent();
        enemyrunAnimation = GetComponent();


    // Update is called once per frame
    void Update()
        if (transform.position.y GreaterSing -10)


    private void OnCollisionEnter(Collision collision)
        if (collision.gameObject.CompareTag("player"))


    void enemySetupPath()
        Vector3 enemyPath = (playerTransform.transform.position - transform.position).normalized;//normalixed means follow the player with narmal given speed not fast and not slow.
        transform.LookAt(2 * playerTransform.transform.position - transform.position);//see enemt to player
        transform.Translate(enemyPath * enemySpeed);
        enemyrunAnimation.SetBool("walking", true);
        //float angle = Vector3.Angle(transform.forward,enemyPath


First we get the player position and substract player position to enemy position the follow the player position. use the LookAT with vector3 to see the enemy with player. It is the simple script to follow the player.

Note: TO follow the player always use vector3 with player position - ENemy position. TO see the player use vector3 LookAT and mutily player position with 2 and substract with enemy position.

Post a Comment