Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Fast Question about lag and Navigation!

Discussion in 'Scripting' started by Zackhanzo, Jun 17, 2014.

  1. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Just 2 fast questions! :p

    1. Lag: i have this simple script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class EnemyAI : MonoBehaviour {
    4. Transform target;
    5. public float moveSpeed = 0;
    6. public float rotationSpeed = 0;
    7. Transform  myTransform;
    8. public    float maxdistance;
    9.  
    10.  
    11.  
    12. void Awake()
    13. {
    14.     myTransform = transform;
    15.  
    16.  
    17.         target = GameObject.FindWithTag("Player").transform;
    18.         maxdistance = 6;
    19. }
    20.  
    21. void Start()
    22. {
    23.  
    24.  
    25.  
    26. }
    27.  
    28. void Update () {
    29.  
    30.  
    31.  
    32.          
    33.         myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
    34.                                                  Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
    35.      
    36.         if (Vector3.Distance (target.position, myTransform.position) < maxdistance) {
    37.          
    38.             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    39.         }
    40.  
    41.                      
    42.  
    43.  
    44.  
    45. }
    1. Why im getting lag with just 1 Enemy with this script? (i need at least 20 enemys on the lvl (not on the screen at the same time) I used awake function so... i dont get why im getting this lag :S



    2. I remember using "Navigation" to tell to the enemys where they can go or not. Now i cant find that option, is disabled on 4.5+ free Unity ? Cant find that option.

    Thanks :p
     
    Last edited: Jun 17, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I don't see anything in that code that could be causing any amount of slowdown. The slowest thing happening there would be Vector3.Distance, but that on its own wouldn't cause anything noticeable until there were hundreds of them around.

    Disable this script on the object. Does the slowdown still happen? If so, there's something else on your object causing it. If not, then something very weird is going on.
     
    Zackhanzo likes this.
  3. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Oh well.... You are totally right. Im newbie with unity as you said there is no problem with that script, and with HealthEnemy Script either.

    The problem is with the Mesh Collider :(

    I mean, i put a Mesh Collider on the root of the enemy (its an animate one) and i wanna make that enemy hit the player with a melee attack if the distance is <1.5 (not sure if its less). The "cube" where i was testing this simple IA was a cube, so it works flaweslly with the box colider.

    So... if its the Mesh Collider cause this lag, how can i solve this? I mean, i need a collider on the body (i can solve that with a capsule collider, but how can i put a collider on the arms?)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Quite simply.... don't used a Mesh Collider for characters. Not only are they slow, but I don't think they animate, either.

    You can use compound colliders - if you add a collider without a rigidbody to a child of an object that has a rigidbody, that collider becomes a "part" of that rigidbody. And those, you CAN animate!
     
    Zackhanzo likes this.
  5. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    I was out for 3 days, thanks for the reply. Now things work properly and without lag!

    So... and how about point 2? anyone knows? I mean i remember with olders versions of Unity i pressed Navigation and go up a menu to modify where a npc could walk/run or not. Im on 4.5 and i cant find that option. There are 3 option on Navigation, and none of them seems that :(
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The NavMesh is what you're thinking of, and that's a Pro feature. At least, parts of it always were, I've never been entirely clear. I think automatic NavMesh generation was Pro? Something like that.
     
    Zackhanzo likes this.