We are having some problems with scripting for the enemies in our game. Instead of stopping when they approach the player and attacking, they continually leap over, turn around, and keep leaping, circling the player and seeming to crawl over the player, and we’ve tried everything we know of to change/fix it. We’re not experts by any means, but we can’t seem to see where the problem is and how to fix it. Included is the script for the enemy. Any help at all would be appreciated.
567895–20151–$EnemyPoliceGuy.js (6.17 KB)
your executing this, inside of the Start Method.
yield WaitForSeconds(Random.value);
// Just attack for now
while (true)
{
// Don't do anything when idle. And wait for player to be in range!
// This is the perfect time for the player to attack us
yield Idle();
// Prepare, turn to player and attack him
yield Attack();
}
ERROR: (this way your code is always stuck inside of the Start Method).
FIX: Move the code, inside of the Update Method, and get rid of the While Loop
I am a complete beginner at scripting in Unity. I got the script from a tutorial and am trying to get it to work for what I need for a project, If someone could elaborate a little bit more on what I need to do, I would greatly appreciate it.
Let me point you to a sorted list of tutorials than:
http://answers.unity3d.com/questions/4897/how-can-i-start-learning-unity-fast-list-of-tutorials/7235#7235
You need to learn to crawl before you can run.
Nobody started running straight from the bat.
I know your hot for the action, but if you don’t have the basics covered that is a sure receipe for disaster later on.
Good luck
That’s the kicker… we thought we were crawling, but in reality we’re stuck. This is for our college capstone project, and they really didn’t prepare us for this kind of thing at all. We haven’t been taught anything along these lines, and the best our instructors tell us is “figure it out,” “go debug it”, etc.
Everything else in the project works, everything looks fine, there are no real “bugs” or errors, the enemy just isn’t doing what we “want” it to do, though it must be doing what it is “told” to do. We’re just trying to figure out how to tell it something else, and everything we’ve tried so far hasn’t worked.
Again, any elaboration into anything that is seen wrong with the script would be appreciated.
nevermind, I found out how to fix it. thanks though.
Well the Problem in your Code is the code flow.
Normally the Code flow is like this (simplyfied):
Awake() → Start() → Update → LateUpdate
Than back to Update → LateUpdate
But your Code is running a While(true) Loop inside of Start()
While(true), means I will never exit this loop (simplyfied)
So your code is stuck in the Start phase.
Tutorial: 2D based Gameplay Lerpz: Will take probally 1-2 days to complete.
__Documentation: http://unity3d.com/support/documentation/ScriptReference/index.html__
**Update **
Update is called every frame, if the MonoBehaviour is enabled.
**LateUpdate **
LateUpdate is called every frame, if the Behaviour is enabled.
**FixedUpdate **
This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
**Awake **
Awake is called when the script instance is being loaded.
**Start **
Start is called just before any of the Update methods is called the first time.
So to fix the Code Flow:
You need, to move the while(true) and everything contained in there inside of your update method/function.
Afterwards delete the While loop.
I appologize for the quality of this posting, I am extremly tired.
And this is the last post I am making before hitting the deck.
If there is some more Questions, I will try to answer them tomorrow.