|
Hi! I'm new to Unity and C# and wanted a little help with this little Script of mine. I'm not quite sure if the title is explicit. So, what I want to achieve is, basically: I have an enemy with a Gun attached to it. This gun has an empty GameObject that should be "shooting" prefabs forward and there should be some time (set by myself) between every shot. Ten seconds after the bullet has been shot, it is destroyed. I have achieved this in my FirstPersonController with right mouse click. But I can't seem to make it automatically. My question is, if I instantiate the enemies when going through a trigger, how can I make them start shooting automatically after being instantiated, given an X time between shots? NOTE: The enemies should just shoot forward, not aiming or anything. And they'd have to do it endlessly. This is what I have so far.
I've also tried using WaitForSeconds, but I just can't seem to entirely get it, though. If someone could help me with this, I'd be really thankful.
(comments are locked)
|
|
This is very simple. WaitForSeconds is the way to go. ShootScript.cs
To create your own timed destruction, you could also use WaitForSeconds: AutoDestruct.cs
or not AutoDestruct.cs Hey, thanks for the quick response! One thing, though. I tried your code.. but it seems to had flooded one of my cores. I had to terminate the Unity process. I re-checked your code but I don't seem to find the cause of the stacking. I'll keep tinkering here. If you find what's causing it, let me know. :) Thanks again.
Oct 19 '10 at 10:13 PM
SantiN90
Yeah, a little too quick - had some typos that I've since fixed. It's your physics that's crashing your Unity. Your bullets are colliding with the one shooting them. You can either ignore collision between the two (as I have updated the script to do), remove the collider from the shooter or offset your instantiate so that it is not inside your shooter's collider.
Oct 19 '10 at 10:46 PM
skovacs1
Well, I've been playing with it. I changed some stuff, like the "newBullet.AddForce(transform.forward * bulletSpeed);" for "newBullet.velocity = transform.TransformDirection(new Vector3(0,0,speed));" using the "public float speed = 50;" I had earlier. It seems to be working almost perfectly now. Except for some cases in which a core gets stacked. Well, Thank you very much for your help. It is much appreciated. :) See you around.
Oct 19 '10 at 11:05 PM
SantiN90
The reason I went with AddForce over velocity= is because the docs state that velocity= can create "unrealistic behaviour". Either way, the net effect is the same if you adjust your forces and neither will actually take effect until the next FixedUpdate anyways. To debug your crashing you could try something like "function OnCollisionEnter(collision : Collision) { Debug.Log(collision.gameObject.name); }" in a script on your bullets to tell you what you are colliding with.
Oct 20 '10 at 02:23 PM
skovacs1
(comments are locked)
|
