- Home /
Why does this simple follow script make my game lag?
It makes my game lag when I apply it to an object. All it's supposed to do is follow my character without modifying the y position so it always stays on the ground. If it collides with my character then my character loses 1 hitpoint. Here's a screen shot of the settings for the rigidbody and other components of the enemy that I applied this script to. http://i.imgur.com/yDpFhAG.png?1 The little crab like things are what the enemy looks like.
It looks like it I remove transform.LookAt(Player.transform.position); it stops lagging. Does anyone know why?
NEW FINDING Changing collision detection to discrete also makes the problem go away, even in the presence of transform.LookAt.
public float Speed;
void Start () {
Player = GameObject.FindWithTag("Player");
transform.LookAt(Player.transform.position);
}
void FixedUpdate()
{
SeekTarget();
}
void SeekTarget()
{
Vector3 MoveTowardsPosition = Vector3.MoveTowards(transform.position,Player.transform.position, Speed * Time.deltaTime);
MoveTowardsPosition.y = transform.position.y;
rigidbody.MovePosition(MoveTowardsPosition);
transform.LookAt(Player.transform.position);
}
Anyone? Is there nothing in this script that raises a red flag?
When you say "lag", you mean becomes less responsive to player input? Or that the frame rate drops? (by how much?) Is this the only script? How many crab objects do you have it attached to?
@tanoshimi The framerate drops. I have 3 crab objects. I've just noticed that the lag only happens when the crabs are near each other. When they are, it varies from 2-10 and then back to 60fps. If I delete 1 crab and only 2 of them are near each other, I don't experience the fps drop. I recorded a video of what happens here: http://www.youtube.com/watch?v=B44nlRaOGPE&feature=youtu.be
Here's a screenshot of the hierarchy of the cube in case it matters: http://i.imgur.com/hg13kT0.png?1?8837 All sub-cubes contain the same components.
Continuous dynamic attempts to alter a RigidBody's position if it's intersecting with another. Since the usual alter direction would be invalid, it is struggling with it. It's also a very expensive algorithm. You'd use it only if you believe an object is moving so fast that it might pass completely through something in between frames.
Answer by Paparakas · Oct 30, 2013 at 09:16 PM
Changing the collision detection from continuous dynamic to discrete worked. Alternatively not rotating the object also made the problem go away. Explanation by @Dracorat as to the why of the problem:
"Continuous dynamic attempts to alter a RigidBody's position if it's intersecting with another. Since the usual alter direction would be invalid, it is struggling with it. It's also a very expensive algorithm. You'd use it only if you believe an object is moving so fast that it might pass completely through something in between frames."
Answer by memblock · Oct 30, 2013 at 06:45 PM
Hmmm,,, not sure why it lags. I played around with a simple 'follow-player' script. It never turned out into anything useable but I simply med use of the Vector3.Lerp command. Check into that,,, maybe it can help you out. Best of luck!
Your answer
Follow this Question
Related Questions
How Do I Reduce Lag in my Networked Game? 5 Answers
Very Bad Lag from ~20 Large Particles 0 Answers
Android lag 1 Answer
WebGl Using too much memory 1 Answer
OnTriggerEnter causing lag spike? 0 Answers