- Home /
The Weirdest Bug Ever: GameObject Transform Appears in Editor to Be in One Spot but Instantiates and Registers in Another
I've linked to a YouTube video showing the bug in action. It is truly weird. For some reason, Unity seems to think that my barrelLocation is actually offset somewhere, so it instantiates objects (and more importantly raycasts!!) at a different spot with a different angle. I have absolutely no idea why this is.
I would love to know how to fix this so I could add some realistic shooting enemies in my game, but this seems to be an error with Unity itself, perhaps because the object is a child of too many objects or because those parent objects are animated.
Video: https://youtu.be/pg_pu8wY4o8
You are using center tool handle position. It does not show the right position of the transform. Change it to pivot.
I changed the tool handle to pivot, but it didn't change anything on the display. Everything was still the exact same. However, I did some digging and experimenting and I found that the error is caused by the gameObject's position in the hierarchy. You can see it here: https://youtu.be/_-W4nuLp7c4
So I did a bit deep inspection on your vid. Where is your line renderer? I'm suspecting that your line renderer is inside your gun. Line renderer has the option to be in world space and local space. The way it fluctuate in a certain pattern indicates to me that it is not in world space and belongs in a transform that moves with your animation.
Never assume it is a issue with unity itself when you are coding something it is the first time you do, you need to share the code for us to help you, you are 100% confusing local and world coordinates
Hey, thanks for the reply. The thing is, there really isn't any code being used. I initially assumed that this error was due to instantiating a prefab when the prefab had a different transform, but then I noticed that it was also projecting raycasts (and the transform was reset for the prefab).
You can see where it starts to fail in this video: https://youtu.be/_-W4nuLp7c4 As you can see, the bug depends entirely on where the object is located in the hierarchy; if I move it up out of the hierarchy, it disappears.
The only code I'm using for this is a simple laser pointer script, which uses a raycast to display a LineRenderer as a method of debugging. All the code on the bodyguard (uppermost parent object) can be removed without changing the glitch.
$$anonymous$$y Discord username is Soareverix#7614. I would really appreciate help or even just a possible alternative. I know that there has to be a way to project a raycast correctly from an animated child object, since many games use it for enemies.
share the laser point script so i can take a look
Answer by unity_ek98vnTRplGj8Q · Aug 27, 2020 at 07:27 PM
I've experienced a very similar bug in my own project that I think was ultimately due to the animation of the parent object. The animation seemed to be placing the objects in weird and unpredictable spots during the update loop, but then their position would settle down after the update loop finished. The fix for me was to move the aiming code to LateUpdate, where the position of the objects matched what I expected them to be. Can you try moving your code to LateUpdate and see what happens?
Dude. You're a GOD! That literally works perfectly! I just basically fell out of my chair and now I'm laughing like crazy typing this because it was literally four letters and now the LineRenderer is displaying perfectly, which should mean that I can aim raycasts now. You are a literal God to me :D Thank you so much XD
Hahaha I'm glad it worked. Still not sure why though, its almost like part of the animation is playing before the update loop and part of it is playing after... I'd be curious to see if anyone can tell us what's going on here.
Answer by Soareverix · Aug 27, 2020 at 08:09 PM
I got it! My problem was that raycasts and objects were instantiating at an offset point from the barrelLocation of my gun. I thought that there must be some way to adjust for the offset or remove it. Well, the offset is caused by the animation doing some weird things in Update() / IEnumerator time. Using LateUpdate(), however, fixes this. So, here was the code I originally wrote (ignore the weird spacing, Unity Answers had trouble displaying it all on the same indent for some reason):
IEnumerator ShootAtPlayer() {
alreadyShooting = true;
yield return new WaitForSeconds(waitBeforeShooting);
if (agent.speed != 0)
{
muzzleFlash.SetActive(true);
RaycastHit hit;
if (Physics.Raycast(barrelLocation.position, barrelLocation.forward, out hit, range))
{
GameObject impactObject = Instantiate(impactEffectWall, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactObject, 2f);
}
GameObject tempFlash;
tempFlash = Instantiate(muzzleFlash, barrelLocation.position, barrelLocation.rotation);
Destroy(tempFlash, 0.2f);
StartCoroutine(ShootAtPlayer());
}
}
The problem is caused by an animation issue that makes all of the child objects behave weirdly on Update() or IEnumerator time.
The solution is to use LateUpdate(). Here's how I got it working:
void LateUpdate()
{
if (shootNow)
{
shootNow = false;
RaycastHit hit;
if (Physics.Raycast(barrelLocation.position, barrelLocation.forward, out hit, range))
{
GameObject impactObject = Instantiate(impactEffectWall, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactObject, 2f);
}
GameObject tempFlash;
tempFlash = Instantiate(muzzleFlash, barrelLocation.position, barrelLocation.rotation);
Destroy(tempFlash, 0.2f);
}
}
Then, I had the 'shootNow' bool set to true when an IEnumerator was called, so that the enemy wasn't firing a thousand shots per second at the player.
This bug took me nearly an entire week to solve (and 23 comments on the post!), but now I have a fully working gun enemy and a much deeper understanding of Unity time, world-space, local-space, and animation root motion.
The Unity community is amazing :D