ok, super low framerat because on THIS script?

i have a model of a dog. while animated in my scene i get 60 fps or more.

now i add a mesh collider, and a ridgid body, and this script

private var leader : Transform;
private var follower : Transform;

var speed : float = 6.0;
var chaseRange : float = 55.0;
var midRange : float = 30.0;
var closeRange : float = 7.0;
private var range : float;

private var walking = false;
private var running = false;

function Start()
{
	leader = GameObject.Find("PlayerController").transform;
	follower = GameObject.Find("dog").transform;

	// Set all animations to loop, and stop any that are playing
   animation.wrapMode = WrapMode.Loop;
      animation.Stop();
}

function Update(){

   // Calculate the distance between the follower and the leader.
   range = Vector3.Distance( follower.position,leader.position );

	if ( range <= chaseRange  range >= midRange) {
	
		if (!running) {
			running = true;
			walking = false;
			animation.CrossFade("run");
		}

      // If the follower is close enough to the leader, then chase!
      follower.LookAt(leader);
      follower.Translate( (speed * 3.15) * Vector3.forward * Time.deltaTime);
	
	}

   else if ( range <= midRange  range >= closeRange){
	
		if (!walking) {
			running = false;
			walking = true;
			animation.CrossFade("walk");
		}

      // If the follower is close enough to the leader, then chase!
      follower.LookAt(leader);
      follower.Translate( speed * Vector3.forward * Time.deltaTime);

   } 
   
   // We have gotten too close  !
   else if (range >= closeRange) {
		if (walking || running){
			walking = false;
			running = false;
			follower.LookAt(leader);
			animation.CrossFade("idle");
		}
	}
	
	// Out of range?
	else {
		if (walking || running){
			walking = false;
			running = false;
			follower.LookAt(leader);
			animation.CrossFade("idle");
		}
	}
   

}

the script is so that the dog will run towards the camera, then slow down , and eventually stop. the dog follows you around.

the script runs fine i get regular fps when not focused on the dog,

but when i do look at the dog, and only when the script is running, my fps drops to around 15. that is way to low!

i set all small objects to a separate layer, and cull them early. i also use compression on anything i can think of, and also try and code things properly. but as soon as i interact with one npc the game died from low framerate :frowning: and only while looking at it. if i look at the dog animation without the extra components making it an npc then i dont get the framerate drop.

any ideas?

i just tested the game on another pc, one with a much better video card, and its going even slower! i use the performance script stock with the terrain demo, and after looking at the dog for a bit my vegetation and trees dissapear :roll:

and when i look away it goes up to 80+

could it possibly be the model? or the script? ugh im confused.

Lookup Coroutines - you do not want looping code in your update function.

Update is called EVERY FRAME, so you are running those loops over and over, and each frame has to wait until you are done before moving on. So basically the engine cannot render another frame until your logic is done (not good).

If you put all that logic in a coroutine (or two or three) then what happens is your update functions continue ON (rendering frames) while your logic code is worked on in pseudo-parallel fashion. So next update comes along and it see’s your logic is still being processed so it keeps going…Once its done processing, the next Update will fire it off again.

Does that make any sense at all? Either way lookup coroutines in the scripting section of Unity’s help.

KrankyBoyGames - I don’t see any loops in Update, just a bunch of conditionals?

stevend - I don’t spot anything that jumps out or should be particularly heavy about your script.

So first thing, does the console show anything? (Errors or debug text printed every frame will slow your game to a crawl)

Assuming there aren’t, try isolating the problem through simple brute force. Comment out the entire contents of Update(). Does the problem go away?

If it does, comment out blocks until you’ve narrowed it down a bit. Most of the time by the time you’ve narrowed it down a solution jumps out at you, and if your still stuck it’ll be easier for others to help you.

ok i narrowed it down.

well i made a new dog, and the only possible difference i could have made was i put the mesh collider on the mesh before and not the prefab, once i changed this, everything worked just fine.

is this a bug? or am i missing out on why this makes a difference.

KrankyBoyGames: code is never executed inside a conditional where the program cannot reach. so it couldent really slow anything down

You don’t want a mesh collider on the dog anyway; use a primitive collider (or compound primitive collider).

–Eric

Sorry not sure what I was looking at there!!!

I looked at those conditionals and saw “while(true)” type syntax…stupid brain.

well i figured i should use primitives, but at the same time if i just use convex, it seems to work out just fine, without too many faces.

thanks for the help guys!