Hello everyone! I have developed a game in which there are enemies that run towards you to kill you. The enemy control system is managed by 3 different scripts: one that deals with managing what the monster does before running to meet you (random movement), one that manages the animations according to the situation, one that manages what happens when you are chasing.
So far I have been working on a 1366X768 screen and the speed of the enemies was perfect. I never had any problems with any game builde, even doing tests on a screen with a different resolution. Now I bought a 1920X1080 screen and the enemies are running towards me too fast. i can’t really understand the problem.
I paste below the scripts of the enemies
This is the first code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class EnemyAI : MonoBehaviour
{
public float speed = 5;
public float directionChangeInterval = 1;
public float maxHeadingChange = 30;
CharacterController controller;
float heading;
Vector3 targetRotation;
void Awake()
{
controller = GetComponent<CharacterController>();
heading = Random.Range(0, 360);
transform.eulerAngles = new Vector3(0, heading, 0);
StartCoroutine(NewHeading());
}
void Update()
{
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
}
IEnumerator NewHeading()
{
while (true)
{
NewHeadingRoutine();
yield return new WaitForSeconds(directionChangeInterval);
}
}
void NewHeadingRoutine()
{
var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
heading = Random.Range(floor, ceil);
targetRotation = new Vector3(0, heading, 0);
}
}
This is the second code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFollow : MonoBehaviour {
private Animator anim;
private Transform player;
UnityEngine.AI.NavMeshAgent agent;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
player = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("IsWalking", false);
if (direction.magnitude > 0.75)
{
agent.SetDestination(player.position);
this.transform.Translate(0, 0, 0.09f);
anim.SetBool("IsRunning", true);
anim.SetBool("IsAttacking", false);
}
else
{
anim.SetBool("IsRunning", false);
anim.SetBool("IsAttacking", true);
}
}
}
This is the third:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class EnemyManager : MonoBehaviour {
private Transform player;
private Animator anim;
[SerializeField]
private string loadlevel;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player").transform;
anim = GetComponent<Animator>();
gameObject.GetComponent<EnemyFollow>().enabled = false;
gameObject.GetComponent<EnemyAI>().enabled = true;
}
// Update is called once per frame
void Update () {
if (Vector3.Distance(player.position, this.transform.position) < 15)
{
gameObject.GetComponent<EnemyFollow>().enabled = true;
gameObject.GetComponent<EnemyAI>().enabled = false;
anim.SetBool("IsWalking", false);
anim.SetBool("IsRunning", true);
anim.SetBool("IsAttacking", false);
}
if (Vector3.Distance(player.position, this.transform.position) > 15)
{
gameObject.GetComponent<EnemyFollow>().enabled = false;
gameObject.GetComponent<EnemyAI>().enabled = true;
anim.SetBool("IsWalking", true);
anim.SetBool("IsRunning", false);
anim.SetBool("IsAttacking", false);
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
SceneManager.LoadScene(loadlevel);
}
}
}