Day 134 — Setting up Enemy AI

private CharacterController _controller;
private Transform _player;
[SerializeField]
private float _speed = 2.5f;
[SerializeField]
private float _gravity = 2.0f;
private Vector3 _velocity;void Start()
{
_controller = GetComponent<CharacterController>();
_player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
if (_controller.isGrounded == true)
{
Vector3 direction = _player.position - transform.position;
direction.Normalize();
direction.y = 0;
transform.localRotation = Quaternion.LookRotation(direction);
_velocity = direction * _speed;
}
_velocity.y -= _gravity;
_controller.Move(_velocity * Time.deltaTime);
}

--

--

--

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Javascript and Single Page Applications

How I’ve been Using Puppeteer

Build dynamic forms using JET! — Part 4/5

Frontend Weekly Digest #237 (22–28 November 2021)

React Lifecycle With Class Components

linkfly.to/hurtadogemelas…

How to Build a Mobile App Using React Native

Setup WSL on Windows 10 for your JavaScript Development with Visual Studio Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Connor Fullarton

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

More from Medium

Creating an Office Den in Unity Part 3: The Icing on the Cake

Enabling Unity Ads

Implementing Unity Ads API

Framing Transposer Virtual Camera Explained in Unity’s Cinemachine