- Home /
I changed my monitor resolution and now my enemies are chasing me too fast ??
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);
}
}
}
Answer by hamerilay · Feb 03, 2021 at 08:38 AM
The problem should be in this line of code:
controller.SimpleMove(forward * speed);
You forgot to add Time.deltaTime, change it to this:
controller.SimpleMove(forward * speed * Time.deltaTime);
Time.deltaTime is the time between this frame and the last frame, this means: the slower the frame rate the larger the speed. It could be that you have to bump up your speed.
Actually I had tried to insert this before, but the result is that the enemy will walk in place without advancing. thanks in any case for the answer! if you can think of anything else you would be of enormous help, i haven't been able to fix it yet
Because you didnt adjust your speed. This solution is the only one that will help your problem. When multiplying with Time.deltaTime you are effectively multiplying with the time between frames, which usually is not far above 0. In order for this to work you have to increase your speed by a factor of like 50. Try multiplying your speed variable with 50 and using Time.deltaTime and you will see it works.
if you tried it and it doesn't work than there is something else wrong with your code, using * Time.deltaTime is how you make things move at a constant rate despite changing frame rates and resolutions.. it is the standard way of doing it and I have done it myself on countless projects. Maybe you need to increase speed?
Answer by Reco3D · Feb 17, 2021 at 03:28 PM
I solved changing this:
this.transform.Translate(0, 0, 0.09f);
In this:
this.transform.Translate(Vector.forward * Time.deltaTime);
In the Second script
Your answer
Follow this Question
Related Questions
Navmesh follow and stop at a distance 2 Answers
How do I make a enemy follow me 3 Answers
How do I get enemies to only chase me in certain areas 0 Answers
Jittery movement of nav meshagent 0 Answers
Enemys following and shooting 1 Answer