• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LordJulian · Nov 17, 2011 at 05:44 PM · collisionraycastdestroybullet

Destroy Turret with machine Gun

Hello.

I have worked on the raycast collider scripts for a few days now, trying to figure out how to allow A (particle emitter) Machine gun script to detect the enemy turret I've set up, which in this see, be able to damage the turret upon contact with the bullet Impact emitter.

When the health of the enemy turret is 0, it destroys itself while causing a small explosion(the explosion prefab from Unity)

However, I've tried to search for several answers to my detection problem, and asked for help from a fellow friend. After several alterations, this is what I have come up with:

[heliMachineGunScript]

     var        gun_Emitter_Object        : GameObject;

var weapon_Fire_Delay : float = 0.0; //this cant be 0!! This var is the delay between the shots

var bullet_Impact_Prefab : GameObject;

var emitter_life : float = 0.1; //the time before the impact prefab gets destroyed

private var weapon_timer : float; //timer

private var bullet : GameObject; //the current bullet we have just shooted

function Start ()

{

 weapon_timer = Time.time + weapon_Fire_Delay; //this resets the timer

}

function Update ()

{

 gun_Emitter_Object.particleEmitter.emit = false;

 

 if ( Input.GetButton( "Fire1" ) && Time.time >= weapon_timer)

 {

             gun_Emitter_Object.audio.Play();

     gun_Emitter_Object.particleEmitter.emit = true;



     var hit : RaycastHit;

     

     if ( Physics.Raycast( gun_Emitter_Object.transform.position, gun_Emitter_Object.transform.forward, hit, Mathf.Infinity) ) 

     {

         bullet = Instantiate( bullet_Impact_Prefab, hit.point, Quaternion.LookRotation( hit.normal ) );

         if(hit.collider.gameObject.tag == "Enemy" || hit.collider.gameObject.tag == "enemy"){

             hit.collider.gameObject.GetComponent(enemyTurretDestroyScript).MachineGun();

             Debug.Log("YES");

             }

     }

         

     weapon_timer = Time.time + weapon_Fire_Delay;

 }

}

[enemyTurretDestroyScript]

 var turretHealth : float;

var explosion_Prefab : GameObject;

function Update()

{

 if (turretHealth < 1) 

 {

     Instantiate( explosion_Prefab, transform.position, transform.rotation ); 

     Destroy(gameObject);

 }

}

function MachineGun ()

{

 turretHealth -= 1;

}

It still doesn't work, nor does it detects the turret when I fire at it.

Addtional notes to know: The Machine Gun is not a projectile based weapon but a particle emitter weapon, with a heli_Gun_Emitter that is active when fired, and the bulletimpact Prefab will appear when the MG script detects a collision on any mesh.

The turret has the "Enemy" tag.

Could someone help me on this script? I'm in a rush to get things done, and would gladly appreciate any pointers from the fellow unity members.

If I'm not clear enough, let me know.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Apply opposite force on mouse-click + collision 2 Answers

How to Destroy an Object while bullet is colliding it 2 Answers

Objects do not destroy on Collision 1 Answer

How to Destroy a gameobject on collision 3 Answers

Trouble with raycast hitting a tagged object 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges