artificial intelligence - 2D Unity Enemy Chase/Evade script in C# -
okay i've been fighting script couple days now. i've made progress in other aspects can't seem enemies chase player character.
the script supposed have enemies wander until empty child 'eyes' sees player. should start chasing player. think pac-man. it's doing right making 1 loop of it's wander cycle , stopping , not seeing player @ all.
this code i've got far script -
using unityengine; using system.collections; public class dudefollow : monobehaviour { transform tr_player; float f_movespeed = 3.0f; private dudemove movescript; public transform eyes; public float sightrange = 3f; // use initialization void start () { tr_player = gameobject.findgameobjectwithtag("player").transform; movescript = getcomponent<dudemove>(); } // update called once per frame void update () { raycasthit hit; if (physics.raycast (eyes.transform.position,eyes.transform.forward, out hit,sightrange) && hit.collider.comparetag ("player")) { transform.position += transform.forward * f_movespeed * time.deltatime; movescript.enabled = false; } }
}
any or tips appreciated.
because have 2d game, happens enemy wonders away on z-axis well, because it's 2d, can't see it. switch 3d mode in main scene window , see if that's case.
if is, reset z-axis 0 on every frame , disable angular momentum :) had happen 2d games.
void update () { raycasthit hit; if (physics.raycast (eyes.transform.position,eyes.transform.forward, out hit,sightrange) && hit.collider.comparetag ("player")) { transform.position += transform.forward * f_movespeed * time.deltatime; movescript.enabled = false; } transform.position.z = 0; // or along lines, don't remember syntax exactly. }
Comments
Post a Comment