I’m trying to make a tranquilizer gun that fires darts. It fires the dart in the right direction most of the time, but some of the darts come out slightly down and to the right.
Here’s my code:
When your dart instantiates does it spawn as a child of the player? I’ve come across this problem myself experimenting with bullet tracers and I think it will either be related to child behaviour or there’s something else going on with the transform as the object tends to follow the player movement if you strafe for example.
In your instantiate line, you use dartSpawner.transform.position, but transform.rotation. Is this intentional? Or does the rotation of your dart matter at all?
What is the viewpoint of this game? first person? third person? topdown? side scroller? Without that info it might be impossible to pinpoint the problem.
Does the dart itself have anything affecting it’s own rigidbody movement?
Thanks for the replies. The game is first person. I used transform.rotation because it was the rotation of the gun but the dartspawner and gun have the same rotation. The only script on the darts is to make them stick to whatever it hits. The issue still happens with the script commented out. The darts are not a child of anything but the gun is. I messed around with moving the gun. If its not attached to anything or to the main character game object, the darts came out fine. The issue only happens when it is attached to a character bone. Also I noticed the darts look like they are spawned away from the dartspawner whenerver they are off.
When it’s asked whether something is a child of another gameobject, it is in the context of hierarchy. Even so, based on the way your dart is spawned in your code snipplet, that should not be the case.
If your dart is being spawned off position, check if your gun, or anything near the dart spawn area, has any collision volumes overlapping the dart. They may be forcing the spawned dart out of place, giving it unintended velocities causing the odd trajectory.
If that is indeed the case, it will be a good opportunity for you to study unity’s collision layers, assuming you don’t know of it.
I have my gun and dart on a separate layer form the player that doesn’t collide with other itself or the player layer. Also when it does spawn in the wrong place its always the same spot. It seems like it does this randomly because it will sometimes do it on the first shot, and will sometimes do it after several. I’ve tried moving the item container to different bones, but it does this unless it is attached to the main player object. I noticed also that the darts spawn off in different places if I attached the gun to a different bone.
Think of it as execution order(though that might not really be the case). Update is called, then the animation happens, then LateUpdate is called. Late Update is around the time when the pixels actually change on your screen, which is why the dart shot in Update is not in sync with the animation.
If this solves the issue, maybe it’s a good enough solution for your purpose. Weird that it’s happening at all though. I suspect the root cause is related to how you are handling movement.