• 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 Nikolay198788 · Apr 22 at 10:16 PM · objectparentbullethole

How do I make bullet holes become children of the object that they hit?

I am making a shooting script that uses ray casts to simulate bullets and I want the bullet holes to move and rotate with the object that was hit (in this case, objects tagged with "wood"). This way the target object can be movable via rigidbody. (the target already has a collider and rigidbody if they need to be referenced).

Here is my shooting code:

  using UnityEngine;

using System.Collections;

public class Full_Auto_Gun_Script : MonoBehaviour { //Gun Stats public float damage; public float range; public float heat = 0f;

 //Cameras
 public Camera fpsCam;

 //Bools
 public bool isFullAuto;
 public bool hasSelector;
 public bool isShooting;

 //Objects
 public ParticleSystem muzzleFlash;
 public GameObject muzzleLight;
 public ParticleSystem gunSmoke;
 public GameObject sandImpactEffect;
 public GameObject woodImpactEffect;

 void Awake()
 {
     heat = 0;
 }

 // Update is called once per frame
 void Update()
 {
     if (heat >= 0)
     {
         gunSmoke.Stop();
     }

     if (heat > 0 && !isShooting)
     {
         gunSmoke.Play();
     }

     if (!Input.GetKey(KeyCode.Mouse0))
     {
         isShooting = false;
     }

     if (heat > 0f && !isFullAuto)
     {
         heat = heat - 0.04f;
     }
     if (heat > 0f && isFullAuto && !isShooting)
     {
         heat = heat - 0.25f;
     }

     
     if (heat < 0f)
     {
         heat = 0f;
     }

     muzzleLight.SetActive(false);

     if (hasSelector && isFullAuto == true && Input.GetKeyDown(KeyCode.B))
     {
         isFullAuto = false;
     }

     if (hasSelector && isFullAuto == false && Input.GetKeyDown(KeyCode.V))
     {
         isFullAuto = true;
     }

     if (isFullAuto == true && Input.GetKey(KeyCode.Mouse0))
     {
         Shoot();
     }

     if (isFullAuto == false && Input.GetKeyDown(KeyCode.Mouse0))
     {
         Shoot();
     }
 }

 void Shoot()
 {
     isShooting = true;

     heat++;

     muzzleFlash.Play();
     muzzleLight.SetActive(true);

     RaycastHit hit;
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
     {
         Debug.Log(hit.transform.name);

         Target target = hit.transform.GetComponent<Target>();
         if (target != null)
         {
             target.TakeDamage(damage);
         }
     }

     //Impact Effects
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range) && hit.transform.gameObject.tag == "sand")
     {
         Instantiate(sandImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));

         sandImpactEffect.transform.parent = hit.transform;
     }
     
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range) && hit.transform.gameObject.tag == "wood")
     {
         Instantiate(woodImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));

         woodImpactEffect.transform.parent = hit.transform;
     }
 }

}

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

1 Reply

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

Answer by Caeser_21 · Apr 23 at 02:32 AM

You have to make a reference to the woodImpactEffect then set it's parent... Currently you are setting the reference object's (in the script) parent... Do this instead :

 if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range) && hit.transform.gameObject.tag == "wood")
  {
      GameObject WoodEffect = Instantiate(woodImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
      WoodEffect.transform.parent = hit.transform;
  }
Comment
Add comment · Show 1 · 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
avatar image Nikolay198788 · Apr 23 at 10:11 PM 0
Share

Thank you so much! it worked. Highly appreciated

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

147 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Weapon passing through colliders (objects)? 0 Answers

Manipulating Instanced Object 2 Answers

SteamVR Door handle .. Door, Hinge Joint not Circle Drive. 0 Answers

Find children in parent 8 Answers

Having Parent Origin Follow Child Objects 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