|
How do I kill an enemy in Unity. I want to shoot a torrent 4 times before it is destroyed. Here is my code for the prefab playshot and I have it attached to my player. var projectile : Rigidbody; var range = 100.0; var speed = 20; function Update () { if (Input.GetButtonDown ("Fire1")) { var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation); instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed)); Physics.IgnoreCollision(instantiatedProjectile.col lider, transform.root.collider); } } What type of code do I attach to the enemy? code for the enemy turrent. var LookAtTarget:Transform; var damp = 4.0; var bulletPrefab:Transform; var savedTime=0; function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); var seconds : int = Time.time; var oddeven = (seconds % 2); if(oddeven) { Shoot(seconds); } } } function Shoot(seconds) { if(seconds!=savedTime) { var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(transform.forward * 3000); savedTime=seconds; } }
(comments are locked)
|
|
//On enemy var timesHit = 0; var bullets = GameObject.FindGameObjectsWithTag ( "bullet" ); for ( var i = 0 ; i < bullets.length ; i++ ) { distance = bullets[];
} This the error message I am getting, Assets/Standard Assets/Scripts/EnemyHits.js(9,23): UCE0001: ';' expected. Insert a semicolon at the end.
(comments are locked)
|
|
var life = 0; function OnCollisionEnter(boom : Collision) {
} just paste this code to your enemy, change the value of highlighted number in IF statement depends on you how many shot you want to destroy it. The script works great just take away the ** (I don't know why you put that)
Jul 29 '12 at 04:23 PM
BigBlob
(comments are locked)
|
Formatted... My solution: Make a script on the enemy, have it so that it has a timesHit var. Have the same script find the bullets (Whether through tags or by names),. Check the distance between the bullets and the enemy. Then, If it hits, make timesHit--, and then if timesHit == 0, than Destroy(gameObject);... Just my preference... Thank you Justin. I will try it right now.
Dec 16 '10 at 01:44 AM
Rodney Rogers
Do I attach it to my player or enemy?
Dec 16 '10 at 01:44 AM
Rodney Rogers
Rodney, it's your code just neat so that other people can help... It's not changed by any means... I'll edit my answer for what I'd do personally...
Dec 16 '10 at 02:05 AM
Justin Warner
Thanks for your help.
Dec 16 '10 at 02:49 AM
Rodney Rogers
var timesHit = 0; function Update () { } do I add if(hits < 4) { var timeHit = 4 timesHit--; } Is is that correct.
Dec 16 '10 at 03:57 AM
Rodney Rogers
(comments are locked)
|
