- Home /
Spawner + AI control
Hello guys,
I'm busy with a Zombie game, And I bump into a few obstacles:
. . . . . . . . . . . . . . . . (FIXED) . . . . . . . . . . . . . . . . .
My weaponless zombies won't attack me, the'll just look at me. I don't know a way to make them hit me. (Close-combat, melee)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Also, the waypoints in my map won't really work, they do not form square when I want them to, and the zombies are plainly walking over them, I'd like them to have a bit of a fully walkable track, like 3(meters?) wide, with as few waypoints as possible. I tried to put very much waypoints down, but it didn't work as suspected. they didn't make a nice line, but walked from waypoint to waypoint in a strange way (only x/z directions), which make it look very .. strange. I'd like them to move in a normal way.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
And last but (probably) not the least problem, I need to have a 'Pick-up' system, that when I walk over a gun, I can use the gun. (so propably something that hides the gun until I walk over a copy, then makes the gun visible for switching (like in the FPS tutorials, when you can switch between the various guns)..)
Anyone that can help me with one of the problems?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BTW, When you make a script or so, please do it in Java? Because I'm pretty much In my learning fase, and I'd like to know some more about it, Also I don't understand C sharp much, It wouldn't teach me much :)
(unless you are not able to, of course ;))
Not to contribute to any ongoing Javascript vs. C# wars that may be going on, but I would recommend keeping an open $$anonymous$$d about C#. $$anonymous$$any of the "elder" users on UnityAnswers seem to prefer C#, which is not me saying it is superior, I'm only saying that the majority of good answers that you get have a high chance of being written in C#. In any event, even if you don't want to learn C#, converting the languages is fairly simple. Just look up any functions from the code inside the manual, and switch between C# and Javascript until you understand how to translate them. They often operate similarly.
@Jason_B,
I know, but I'm still busy exploring JS, Don't want to force myself into a hurry of learning both.
@unitydev0008, thanks for the code,
I merged it into my game and it works just fine ;)
again, Thank you for your time and help.
Glad to help out Living Dead, still workin on the weapon pickup stuff so ill post it when i can if its still unanswered, may be a few days tho as im rather busy =P
Answer by unitydev0008 · Mar 05, 2011 at 08:31 PM
One way you can handle the melee combat is to use Vector3.Distance to check the distance between the zombie and its target, if its below the melee range then attack.
Now you would also want some sort of a swing timer so the zombie doesn't attack every frame.
Note this is in C# but shouldnt be too hard to convert over to JS, im not familiar with JS sorry but it should also give you some knowledge having to convert it.
C# Example: // These would be assigned from within the editor public GameObject target; public float meleeRange; public float attackSpeed; public float damage;
private float attackTimer;
void Update() { // If within melee range attack the player if not move towards the player if (Vector3.Distance(target.transform.position, transform.position) < meleeRange) Attack(); else transform.Translate(Vector3.forward * moveSpeed* Time.deltaTime); // Or however you move your zombie here ^
}
void Attack() { // If attack timer is not on cooldown attack the target if (Time.time > attackTimer) { // Or place your code to handle the damage dealing here target.SendMessage("ApplyDamage", damage);
// Resets the attackTimer so the zombie has to wait til next attack
attackTimer = Time.time + attackSpeed;
}
}
Asfar as the other two questions i am unsure of how you are handling the waypoints, and the weapon pickups is a little more involved. I am currently working on a project that should be in Asset store soon on making a top down zombie shooter which has weapon pickups like you are asking about, i may post the code for it here when i get it done if noone else has answered that part. (Just the weapon pickup code that is)
Hope this helps and it may not be the best way so if anyone else has a more effiecent way im all ears =P
I'll "endorse" this answer. :P This is pretty much the way I would do it. If you want to get even more technical, you could ensure that the zombie is facing the player before initiating a melee attack, but this may work itself out naturally since zombies are fairly single-$$anonymous$$ded and probably not turned away from you... a simple way to do a distance and directional check in one is to fire a ray, but rays are precise to a fault in some instances, so this might not be desired.
Answer by Living_Dead · Mar 07, 2011 at 06:57 AM
- Progress:
- Attacking: **( FINISHED )**
- Waypoints: **( FINISHED )**
- Pickup: **( BUZY )**
- Attacking:
FINALLY GOT IT TO WORK!
Yay, now I'm happy :P problem appeared a time ago, It was actually a VERY simple and minor change. I had to change
var Target = GameObject;
into
var Target = transform;
Stupid I did not get that earlier... Actually a pretty stupid mistake :)
But GameObject is the same as transform right? only you need a Gameobject to put it in gameobject.
//******** .Js Scripts coming ********//
- Waypoints:
I got happy with the actual result of doing nearly nothing. I haven't done anything strange to the Waypoints, just was joking around with the walking script of my zombie, then I made it make a small U-turn when I came to see it (16 rays * shaped). just lowering the turning speed and made it move while it turns.
//******** .Js Scripts coming ********//
Transform is a component of a GameObject, Reason I used target as a GameObject in my script is because i used a Send$$anonymous$$essage which requires the GameObject to send it to, cant be sent to a transform(that i know of) But yes using target as a transform works well too, just wasnt sure how you handled your damage. When you drag a gameobject over the variable Target which is a transform it is storing a reference to that GameObjects Transform component. Glad your making progress though =D
Your answer
Follow this Question
Related Questions
zombie ai script 1 Answer
Follow Player Via Waypoints 1 Answer
Enemy AI Range Detection 3 Answers
FPS RIGIDBODY PROBLEM 1 Answer
Complete AI Tutorial? 1 Answer