Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Sindario · Dec 05, 2013 at 03:27 AM · lightingshootingtimescale

Help with a Script in C# for shooting and lighting

Hey everyone basically I need a C# script for hitting object with bullet so when it hits above the object will appear area light. This is what I've got.

using UnityEngine; using System.Collections;

public class ThermalDetonator : MonoBehaviour {

 float lifespan = 3.0f;
 public GameObject light_prefab;
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update (){
     
     if(gameObject.tag == "Enemy") {
         gameObject.tag = "Untagged";
         Instantiate(light_prefab, transform.position, Quaternion.identity);            
     }
 }

}

and this is attached to Bullet prefab that my PC shoots with. And I want it to make so when it hits "target" the name of object tagged as "Enemy" the area light will appear around it and if possible it will go off after lets say 30 second. If anyone could help me with that. I do realize that this script might be useless. Thank you.

Comment
Add comment · Show 1
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
avatar image Fornoreason1000 · Dec 05, 2013 at 04:19 AM 0
Share

ok so you want light to appear when a bullet hits something? what are you trying to do? is this for a sticky grenade type of thing? your Update code doesn't make sense to me.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GerardD · Dec 05, 2013 at 04:36 AM

One approach would be to have a collider on the enemy object that should it be hit by a bullet, it activates an attached light.

This is a quick attempt at a script that would be attached to an enemy. When the enemy trigger is hit by the bullet, then it would activate the light.

 public class Enemy : MonoBehaviour {
 
     //This is the length of time the light will be on for
     public float lightDuration;
     private Light areaLight;
 
     void Start () {
         //This allows you to access the light that is attached to the enemy
         areaLight = GetComponent<Light>();
 
         //Ensures that the light is off be default if you haven't set it up this way
         areaLight.enabled = false;
     }
 
     void Update() {
         //Used for testing - this would be removed
         if (Input.GetKeyDown(KeyCode.Space)) {
             TurnLightOn();
         }
     }
 
     //When the trigger is hit by the bullet
     void OnTriggerEnter(Collider other) {
         //The collider of the bullet has hit the enemy
         if (other.gameObject.tag == "bullet") {
             TurnLightOn();
         }
     }
 
     private void TurnLightOn() {
         StartCoroutine(ActivateLight());
     }
 
     private IEnumerator ActivateLight() {
         //This will turn the light on
         light.enabled = true;
 
         //This will cause this function to wait for the light duration
         //whilst still allowing the engine execution to continue
         yield return new WaitForSeconds(lightDuration);
         
         //This will turn the light off
         light.enabled = false;
     }
 }

This could be attached to whatever GameObject you wanted to have a switch able light on. You might wish to change the logic that calls the TurnLightOn function.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Hey can anyone help with a shooting script??? 0 Answers

Bullets sometimes shoot under map 1 Answer

Shoot Script Help 1 Answer

Shooting speed help! 1 Answer


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