How to adjust the gun so it always shoots where the sight is

3

I have a gun that is positioned on the screen manually and I need to create a script in such a way that when I shoot, the bullet will reach the point where the target is targeting, which in this case is the coordinate of the half of the screen. >

I would also like to know how I can position the gun (via script) so that the barrel is aligned with the crosshair.

    
asked by anonymous 26.07.2016 / 16:07

1 answer

2

It's been a long time since I started Unity3D, but I think I can do this using the Physics.Raycast .

The solution would be to make the gun always aim at the point that is well ahead of the shooter before you fire the projectile:

 RaycastHit hit;
 bool bloqueado = Physics.Raycast(transform.position, transform.forward, out hit);
 arma.transform.LookAt(bloqueado ? hit.point : transform.forward);

After that, you create the projectile, using the same Quaternion of rotation of the weapon and position it well on the tip of it.

    
26.07.2016 / 16:43