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 Major · Feb 20, 2013 at 03:33 AM · textureraycastbulletplanehole

Parenting Bullet Holes?

I have a prefab object that is a plane with a bullet hole texture on it. I can shoot objects just fine and spawn bullet holes where my ray cast hits, but my bullet holes are not following the object that I shoot. I am not sure what to do. What I do know what to do is to use the lines of code in the given piece of code:

 if(decalHitEnemy && hit.transform.tag == "Enemy")
         {
             Instantiate(decalHitEnemy, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
             Instantiate(enemyHitParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
             
         }
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
2
Best Answer

Answer by Benproductions1 · Feb 20, 2013 at 03:41 AM

What you have to do is parent the bullet holes to the object they hit.

Example:

 //Lets make the prefab a Transform for ease of use
 var prefab:Transform;

 // At the start of your script create a temporary variable
 private var tmpObject:Transform;
 
 //When you instantiate you set the return to that variable
 tmpObject = Instantiate(prefab, /*your code here*/);
 
 //You then Make what it hit, the parent
 tmpObject.parent = hit.transform;
 
 //If you'r prefab is of a type other than Transform, you will have to access the transform before you can apply the parent
 tmpObject.transform.parent = hit.transform;

If your objects scale is changed, you will have to come up with a work around, but for now it will work

Hope this helps, Benproductions1

Comment
Add comment · Show 7 · 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 Benproductions1 · Feb 20, 2013 at 03:42 AM 0
Share

A quicker, more ugly way is to set it directly: Instantiate(/your code here/).parent = hit.transform; It might be a bit faster but more ugly code... your choice :)

avatar image Major · Feb 20, 2013 at 03:59 AM 0
Share

I get an error when I shoot my enemy: $$anonymous$$issingFieldException: Field 'UnityEngine.GameObject.parent' not found. And I get this error when I use Transfrom: Assets/BulletScript.js(23,48): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.

Also either way the bullet hole won't parent.

What do I do?

avatar image AlucardJay · Feb 20, 2013 at 04:26 AM 0
Share

$$anonymous$$ake sure you both typecast correctly. The answer is expecting a type of Transform to work, so it should be :

  tmpObject : Transform = Instantiate(/*your code here*/);
  tmpObject.parent = hit.transform;

in the answer, decalHitEnemy needs to be of type Transform. If decalHitEnemy is of type GameObject, use this :

  var tmpObject : GameObject = Instantiate( decalHitEnemy, /*your code here*/);
  tmpObject.transform.parent = hit.transform;
avatar image Major · Feb 20, 2013 at 04:29 AM 0
Share

There we go. Work perfect, thanks!

avatar image Benproductions1 · Feb 20, 2013 at 04:32 AM 0
Share

I've edited the answer to include typecasting

Show more comments

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

11 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

Related Questions

How to stop textures flickering 2 Answers

Destroy Turret with machine Gun 0 Answers

How can I add a distance cap to this script? 3 Answers

Ricochet Problem of projectile bullets 0 Answers

EteeskiTutorials' bullets (raycast bullets with gravity) 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