Hello,
as the titles I have an extremely strange behavior with bullet fire system.
What happens is that the bullet is correctly instantiated and moves when fired.
But for some short time, if I move the camera it follows it.
Then it kind of overshoot in the target direction.
I implemented something very simple, actually a simplified version of this tutorial:
Well most implementation are the same.
I get the center point of the screen with fpsCamera.ViewportPointToRay(0.5f, 0.5f, 0)
Calculate the direction: hit point - spawnpoint
Then instantiate the bullet and addforce to it.
Below is my code.
If you have any idea what could cause this issue I would be grateful
public void fire()
{
RaycastHit hit;
Vector3 targetPoint;
if (animator != null)
animator.SetTrigger("FireTrigger");
// calculate travel direction of the projectile
Ray ray = fpsCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
if (Physics.Raycast(ray, out hit))
{
targetPoint = hit.point;
}
else
{
targetPoint = ray.GetPoint(75f);
}
Vector3 directionWithoutSpread = targetPoint - spawnPoint.position;
directionWithoutSpread.Normalize();
GameObject currentBullet = Instantiate(bullet, spawnPoint);
Rigidbody bulletRb = currentBullet.GetComponent<Rigidbody>();
bulletRb.AddForce(directionWithoutSpread * ShootForce, ForceMode.Impulse);
}