- Home /
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.
Have you tried making them children of the GameObjects they're supposed to be imprinted on?
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);
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?
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
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).
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
Follow this Question
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