Hi,
Here is my situation. My player will have a weapon and it will shoot a projectile at every enemy on screen. The number of enemies on screen will vary throughout the level so what I need scripts that do the following
Weapon script
Count enemies
Instantiate one projectile per enemy
tell that projectile to target that enemy
Then the projectile will need to register which enemy it is targeting and go towards that enemy at a set speed.
I’m getting better at coding but I have no clue how to write the script that will count the number of enemies on screen. I am starting under the premise of using “GameObject.FindGameObjectsWithTag” and look for every enemy with the tag of “enemy” but after that I don’t know what to do.
Any help would be appreciated.
create an array and place each enemy in that array, you could also name each enemy by number
create a for loop that will run through the entries of the above mentioned array. You will use the of the loop to assign new targets etc. 3) inside each loop: a) get the name of the enemy b) instantiate a bullet as gameobject c) give the bullet the current enemy as taget 4) through the next loop everything will increment by one, so new enemy, new bullet and new target get the idea? shout if you need more help, there are tons of info on arrays and the for loop
Thanks guys,
I’m getting awfully close. Using your guys information along with the script reference for “GameObject.FindGameObjectsWithTag” I was able to figure out this far
and then this for the missile
it works great. now just to fine tune it. I would like the missile to go forward and blend to it’s target so the player shoots them all forward and after a specific distance the missiles all veer off in separate directions towards their own individual targets.
Wait, I think I realized what i need to do. I need to move the missile forward and rotate it over time to look at the target over time, so the missile is always moving forward just looking closer towards it’s target thus moving in that direction.
you can also apply the lookat camera function to the Update function of a script on the missile, with the enemy of choise as the target (of the LookAt)
It´s 99% awesome… Everything is working but one minor detail and I can´t figure out. After loading the scene for the first time and firing your first set of missiles the very first missile does not get told what it´s target is. Every other missile in the first barrage gets it. After the first barrage all missiles work great. But my coding experience is tapped. I´m looking for ideas.
Launcher Script
Missile Script
On Instantiate I want the missile to be facing the direction of the object shooting but I find if I have it rotate around the forward axis the missiles “spin” off in various directions and it looks less programmed. I don´t know how to do that in the Instantiate script so I added some random rotation in “function Start” for the missile. Is there an easier way to do that?
Everything should be in the attached unity package. I have never posted a package before though, hopefully that works too
A few things to know. In the test scene there are spheres tagged as “Enemy” that is what the shooting script looks for to fill the array with. You set the speed of the missile in the inspector on the prefab. you must set a damping amount, that is the amount you start with, then I have it double after ´dampingdouble´ seconds up to a max of 20.
The missile instantiates with a slight position change vs the other missiles that way they all don´t go from the same spot.
This is heavily based on what people said above, and almost a complete rip from the smoothcameralookat script with a few addons of my own.
if(Vector3.Distance(transform.position, target.transform.position)< minDist){
//minDist is set by the user
//here u can instantiate the particle effect for explosion and such
Destroy(gameObject);
}
I see it… In your code, you are getting the prefab for the missle, then instantiating it, then changing the target on the prefab, the moving to the next one.
Change the code to correctly create the object on a variable and it will fix your problem.
//Launcher.js
var missile:GameObject;
var enemies : GameObject[];
var target:GameObject;
function Update(){
if(Input.GetButtonDown("Fire1")){
enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (var target : GameObject in enemies){
var newMissile = Instantiate (missile, transform.position+
Vector3(Random.Range(-0.5, 0.5), Random.Range(-0.5, 0.5),
Random.Range(-0.5, 0.5)), transform.rotation);
newMissile.GetComponent("Homing_Missile").target=target.transform;
}
}
}
I cleaned this stuff up a bit too. Use code tags when posting code, this could have been found fairly quickly.
//Homing_Missile.js
var target : Transform;
var damping:float;
var dampingdouble:float;
var smooth = true;
var speed:float;
function LateUpdate () {
if (target) {
if (smooth){
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}else{
// Just lookat
transform.LookAt(target);
}
}
}
function Start () {
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
transform.Rotate(Vector3.forward * Random.Range(0, 360));
}
//while (damping<=20)
//{
//damping=damping+damping;
//yield WaitForSeconds(dampingdouble);
//}
function Update(){
rigidbody.velocity = transform.forward * speed;
}
Thanks BigMisterB for cleaning that up. With just a bit of tweaking now that it’s in my game the results are fantastic…
feel free to snag it up chubbspet. I love the community on here. There are just so many free scripts that people are nice and share. and everyone’s willing to help polish off a script so it’s efficient.
Next weapon is the lightning gun… but that’s for another thread and another day (probably tomorrow)