Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a camera class, that is suppose to chase my player, which works, but its really really slow. It only has this line of code, which makes sense to me, player moves, camera moves. Any idea of why my game runs slower.

void Update()
{ 
    transform.position= new Vector3(player.transform.position.x, player.transform.position.y, -10);
}
share|improve this question
1  
"Slow" as in low framerate? Or delayed reaction to player movement? Or what? If it's framerate, I suspect the camera has little/nothing to do with it. If it's a delayed reaction, Spooks is right about using LateUpdate(). –  chaosTechnician Nov 3 '11 at 3:43

2 Answers 2

up vote 9 down vote accepted

I think the reason this is happening is because you are updating the player and the camera in a random order, which may make the camera move first, which wouldn't cause it to move at all, then the player would move, and the next update the camera has to catch up to the player, which causes a lag like appearance.

Using LateUpdate() on your camera instead of Update() should fix this problem, as you want to edit the movement of the camera after the player has moved first (creating a chasing camera)

Take a look at the Update Order

share|improve this answer
    
As far as I've seen, the ordering is non-deterministic, but it isn't going to do something like alternate ever frame. But putting it in LateUpdate is good advice regardless to ensure update order (or by using the new update order stuff in 3.5 to explicitly set it). –  Tetrad Nov 3 '11 at 2:59
    
you are probably right with it not alternating every frame. But you can't pick which one updates first, which is needed, in order to have a chasing camera. I haven't looked into the new order stuff in 3.5 (haven't got an update yet for it) but i'll look into that as well –  Spooks Nov 3 '11 at 3:31

When you say "really really slow", what exactly do you mean? Are you running this on a mobile device? On the PC it shouldn't matter. Are you going from like 1000fps to 750fps or something?

Is this showing up in the built-in profiler?

Regardless, one speed optimization you can try, if all else fails, is by caching off your transforms.

The built in unity .transform property is quite literally doing return GetComponent( typeof( Transform ) ) (or its equivalent). Storing off a local variable to both your transform and the player's transform will prevent that lookup every time you call it.

share|improve this answer
    
running on pc, my framerate was okay(100-115fps), but it just very laggy. LateUpdate fixed this –  Johnny Quest Nov 3 '11 at 16:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.