Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 impurekind · Aug 22, 2018 at 04:52 PM · movementenemydecalattach

How to make bullet decals move with moving object?

FINALLY! Resolved! Here's what I used (added inside and after the whole raycast check for shooting/hitting an object and stuff):

GameObject theObjectHit = hit.collider.gameObject;

GameObject temp = Instantiate(bulletDecal, hit.point, Quaternion.FromToRotation(-Vector3.forward, hit.normal));

temp.transform.parent = theObjectHit .transform;

And the most important part, which took days to get a proper answer for, was that I needed to assign a temporary variable when instantiating the decal object so I could then use that temporary variable to assign the parent to the decal. Without that one "temp" part it was just errors constantly or it not doing what I wanted it to do.

Original post:

I want to be able to shoot things like the enemies and have the bullet decals appear on them, and when the enemies move I don't want the decals to just be floating in the air, which they currently do.

Does anyone have a nice and simple way of doing this they could share with me?

Note: When I say "enemies", I'm obviously going to make decals appear on pretty much anything I shoot, and an enemy is just an example of one thing in the game that might be moving. There's also the likes of automatic doors and crates that break and change shape as well. So I want the code to be workable on all those kinds of things too.

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 TreyH · Aug 22, 2018 at 08:42 PM 0
Share

Have you tried making them children of the GameObjects they're supposed to be imprinted on?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Casiell · Aug 22, 2018 at 09:56 PM

When you spawn a decal, set it's parent to the object you hit.

According to documentation you can just pass the object you hit as another paramether in instantiate.

You can also set the parent later:

 decal.transform.SetParent(ObjectYouHit);


Comment
Add comment · Show 9 · 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 impurekind · Aug 23, 2018 at 08:57 AM 0
Share

This sounds like it should be useful. Is "objectYouHit" a built-in Unity thing so I can use that line of code exactly as is, or do I somehow need to set that up somewhere by getting the object I hit at the particular time and assigning it to a variable or something like that, and if so, how do I do that?

avatar image Casiell impurekind · Aug 23, 2018 at 11:36 AM 0
Share

ObjectYouHit is just a name I thrown it for an object you deteced yourself. Good thing is that it's easy to detect it.

In general you detect collisions with unity method OnCollisionEnter. Unity gives you CollisionData that has gameObject property. That gameObject is exactly what you are looking for

avatar image impurekind Casiell · Aug 23, 2018 at 12:32 PM 0
Share

I need to be clear that I can't work out the full sequence of what I need to code here unless I see an example (it's just something I'm not good at figuring out with this code stuff until I see a complete example done right).

What's an example of a full sequence in code where I check for the object I've hit and then assign it as the parent of the decal?

Note: I have already done the "raycast" part, so I have a "hit" thing at that part, which is where I create the decal, inside of the whole raycast check part of my script (at the "hit" point).

Show more comments
avatar image impurekind · Aug 26, 2018 at 12:00 AM 0
Share

O$$anonymous$$, I tried just using the example of setting it after fact that you mentioned (as it's the only one I can even remotely work out how to put in there as it pretty much appears; I can't really figure out how to do the other version), and it came up with a red error message "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption." (Note: Any variables you see that have to be "declared" or whatever have been done so at the start of the script as public variables and I've dragged the appropriate objects into the Inspector to assign them). Here is the actual code I'm using:

 private void RaycastGun()
     {
         // Bit shift the index of the layer (11) to get a bit mask (used below to make sure we don't shoot player collidor)
         int layer$$anonymous$$ask = ((1 << 10) | (1 << 11));
 
         // This would normally cast rays only against colliders in layer 11.
         // But ins$$anonymous$$d we want to collide against everything except layer 11 (the player collider is on layer 11). 
         // The ~ operator does this, it inverts a bitmask.
         layer$$anonymous$$ask = ~layer$$anonymous$$ask;
 
         RaycastHit hit;
 
         if (Physics.Raycast(shotOrigin.position, shotOrigin.forward, out hit, 1000, layer$$anonymous$$ask))
         {
             GameObject theObject = hit.collider.gameObject;

             Instantiate(bulletDecal, hit.point, Quaternion.FromToRotation(-Vector3.forward, 
             bulletDecal.transform.SetParent(theObject.transform);   
         }
     }

Edit: I also just tried this variation of the code line above (based on what I found with some more searching): "bulletDecal.transform.parent = theObject.transform;", and it too comes up with the same error.

I've been trying variations on this for a couple of days now, with some of the other stuff you can see in my other comments, and it really is beco$$anonymous$$g a joke that it's taking so long to get something to work that, imo at least, should ultimately be relatively simple and straightforward--but how to do [write] this in Unity/C# is just utterly bewildering to me.

$$anonymous$$an, all I want to do is get some decals to face in the correct direction out from and move with the colliders they're basically created on (Note: I can manage the first part, but I have no clue how to write the add to then do the second part of top of that where it won't come up with an error in either the script or Unity's console or just not work as expected).

I could really do with a proper solution here. :-(

Edit 2: Finally, the problem is sorted. See my amended original post if you want to know how I managed it in the end.

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

145 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

Related Questions

Enemy stops when player on higher place 1 Answer

2D Random AI 1 Answer

Canduct and movement of an enemy 0 Answers

Lock onto a target 1 Answer

How to make an enemy move along a wall? 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