Skip to content

Instantly share code, notes, and snippets.

@didacus
Created November 14, 2021 15:11
Show Gist options
  • Save didacus/5a9c9c851e2c43329921cee2508c048b to your computer and use it in GitHub Desktop.
Save didacus/5a9c9c851e2c43329921cee2508c048b to your computer and use it in GitHub Desktop.
Unity - Enemy controller that follows player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
private Rigidbody RB;
public float moveSpeed;
public PlayerController thePlayer;
// Start is called before the first frame update
void Start()
{
RB = GetComponent<Rigidbody>();
thePlayer = FindObjectOfType<PlayerController>();
}
void FixedUpdate()
{
RB.velocity = (transform.forward * moveSpeed);
}
// Update is called once per frame
void Update()
{
transform.LookAt(thePlayer.transform.position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment