- Home /
The question is answered, right answer was accepted
enemy ai movement.
hi, am doing a space shooter game, in which i want the enemy to move in a zig zag movement, (i.e) it has to move forward and at the same time it has to move left and right. i have tried with physics, but this time i dont want to do it with physics. a basic outline of how to achieve will do great.
Waypoint translations $$anonymous$$eaning, you should develop a set of waypoints(transform or even just vector positions)...these should then be "Lerp`d" towards as your enemy object reaches the next current waypoint.
So, to keep it as a basic outline.... 1: Create an array of way points that can be iterated through. 2: Iteratively
foreach(Transform waypoint in waypoints)
{
transform.position = Vector3.Lerp(waypoints[i].localPosition.x,waypoints[i].localPosition.y, waypoints[i].localPosition.z, Time.deltaTime);
}
or
for(int i = 0; i < waypoints.Length; i++)
{
Vector3.Lerp(waypoints[i].localPosition.x,waypoints[i].localPosition.y, waypoints[i].localPosition.z, Time.deltaTime)
}
Applying these way point positions as the translational goal for your enemy object.
You will need to check the enemy has both reached its current goal and is reassigned to position itself at the new one..
i write this and now have got a bee in my bonnet soam gonna quickly go into Unity.
BRB :)
But.....Read into what ive said here, try and implement it yourself first.
I will be online for a bit, so get back asap if you can buddy, we`ll try and solve this for you hey
Gruffy
Answer by Gruffy · Dec 19, 2013 at 09:24 PM
Hey bud, here is a complete package that i put together.
It translates and rotates the object through a set of waypoints (you can move these to whereever, you like. have a play about with it and see what you come up with)
note: this uses quaternions also to rotate the object tracking the way points towards that waypoint`s local forward.
if you download the zip, unzip and add this custom package to a unity project and run the scene included, you will see the result.
I am guessing you will be wanting to create some sort of state system to handle your enemy AI for that i would create an empty parent(as can be found in the package scene)and i would prefab it out so that the scripts and way points are always inside (or childed) your enemy game object.
This way , when you create said FSM(or such like) when an enemy state was invoked or something like that, your enemy could just start patrolling the waypoints....until it saw you or something that might change its current state if ya get me (this is totally totally totally another subject though - FSMs i mean) Anyway, here`s package with scene and all code - enjoy and if you think it has helped you mark it an answer as i think this might help others too...especially as its a convenient package demonstration etc.
Below is a Link to a Dropbox folder where you can download the package as unity Answers doesn`t seem to be giving me any other options except to answer in text only, n all that.
LINK (copy below URL and paste in browser address bar to get to package)
https://www.dropbox.com/sh/hmn4ax6swykktf7/TuzjsQp-Un
LINK
Merry non-denominated holiday! Thanks for reading. Gruffy :)
Follow this Question
Related Questions
AI Enemy Follow Player 2 Answers
Enemy Follow AI problem 0 Answers
need help in the instantiation script 1 Answer
Character Animation won't work with FPS scripts 1 Answer
makeing enemies 1 Answer