Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Reco3D · Feb 02, 2021 at 04:13 PM · enemynavmeshresolutionfollowenemy ai

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);
         }
     }
 }
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Reco3D · Feb 17, 2021 at 11:32 AM 0
Share

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

avatar image AbandonedCrypt Reco3D · Feb 17, 2021 at 01:18 PM 1
Share

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.

avatar image logicandchaos Reco3D · Feb 17, 2021 at 03:14 PM 0
Share

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?

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges