- Home /
Lag type issue?
I am making a game, and It was working just fine. My computer has 16gigs of ram. 2TB of HDD space, 4.2ghz processor. So I highly doubt my computer is the issue since i can do just about anything on it and there is not much stuff in the scene so far. I can't figure out why all of a sudden it has a lag? or some sort of conflict that is making everything glitchy. I found that the player is the issue. The movement script laggs the game dramaticly. What in this script is causing the problem?
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update()
{
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= speed * Time.deltaTime;
rotation *= rotationSpeed * Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A)
|| Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D))
{
animation.Play ("Run");
} else
{
animation.Play("Idle");
}
}
}
Answer by whydoidoit · Mar 14, 2014 at 08:45 PM
You are calling the animation system every frame. That's not a good plan. Presumably you are starting the Idle animation every frame again and again - also when you press a key you are starting the Run animation every frame the keys are held down.
You want to call the Play on the animation when something changes, not every frame.
Hold some kind of state for the current mode of the character. Like an enum variable or an int.
When you are in the idle state detect a move to run, when you stop running move back to idle. Play your animation on each of those changes. Or use mecanim which kind of does this for you.