Hi everyone, I have a problem with the enemies in my game - they are sometimes not shooting at the player (at the right direction), but when I make a ray in the same direction - it looks like the Vector3 is fine, and I don’t know how to fix the problem. I’d appreciate some help. Thanks!
One thing to note, referring to the Transform as the absolute authority on the pose (position/rotation) is always a common mistake when using physics. The Rigidbody is the authority, it writes to the Transform after all. Read the position/rotation from that component.
For instance, when you use interpolation, the Transform will be interpolating from the old pose to the current one, it won’t be where the Rigidbody is actually at.
Here you can see the red ray going towards the player (the right direction) but the projectile is going to the left (obviously it won’t hit the player) The right screen is first person perspective as the player and you can see the projectile is not coming towards me as the player
Does the projectile perhaps collide with the enemy itself (or any of its child gameobjects)? That is also a common mistake. It may work most of the time, but for example, if the enemy is also moving towards you while shooting the enemy capsule may collide with the projectile the moment it spawns, causing it to go in a different direction.
Right, I didn’t know what that was; i was only looking at the ray.
Yes, I would say check the above that it’s not initially contacting the enemy. Also, it’s odd to not normalise the direction before scaling as you do. This means it’ll be a larger impulse depending on distance; of course you may want that.
Also, you can simply set the velocity of the body here if you’re not adding other forces. At least as a test.