- Home /
Duplicate Question
instantiating a projectile continually over time
I have a prefab moving left to right across the screen I can get it to shoot at the player/camera on the z axis once at the start but I would like to have the enemy object keep shooting at the player as it moves across the screen. I have tried different things but I can't seem to get it to work. Any help would be great.
var projectile : Rigidbody; var initialSpeed = 20.0;
function Update() {
{
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, initialSpeed, 0));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}
}
Your question is unclear. It look like you will spawn a new projectile each frame, but you don't change where you aim the projectile. Is you question an ai$$anonymous$$g issue?
as it turns out, you don't use instantiate during play in video games. you'll need to learn about pools
here is a long discussion
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Answer by Benproductions1 · May 24, 2013 at 12:08 AM
Hello,
Since your question is so broad, I'll just give you a just as broad answer :)
One solution would be to make a timer, every time the timer reaches or goes below 0
, you shoot and reset the timer. Thats it.
Hope this helps,
Benproductions1
Answer by FL · May 24, 2013 at 12:17 AM
Put instantiatedProjectile.LookAt(player)
after the Instantiate.
I suggest you to put this code in the FixedUpdate()
instead of Update()
and use transform.LookAt(player)
(for the enemy tranform) before the Instantiate.
Also, put a gap between the projectiles or they will collide with themselves.
Hi Guys
Thanks for the input. I played around a bit with your suggestions but keep getting errors. It's just over my head. Think I will just have to pay for some scripting.
Regards Thor
Follow this Question
Related Questions
Prefabs wont instantiate using a for loop? 1 Answer
instantiate prefabs/gameObjects 1 Answer
How to instantiate the prefabs on grid in Unity? 1 Answer
Instantiate Reference Problem 1 Answer
Dictionary Keys to Stack Enemy Types 1 Answer