Question by
PaulDabest · Jun 29, 2016 at 05:43 PM ·
shootingaimingcrosshairfirst person shooter
How to make gun shoot in the crosshair
Hello guys, So im really new in unity & coding and i want a bit of help. I have a script that i found in a tutorial in youtube where i can shoot a single bullet and the bullet will go where the weapon is pointing. The problem is that i want it to go perfectly in the center of my screen, where i have made my crosshair.
Also I don't have a bullet "Object". Also the script is on the gun itself and i have a separate script where i basically say, where i press "Fire1" it would play an animation and a sound. I don't know if this will help but i thought it may have to do with something...
You can take a look in the script below. Thanks anyways :)
var DamageAmount : int = 32;
var TargetDistance : float;
var AllowedRange : float = 100;
function Update () {
if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
Shot.transform.SendMessage("DeductPoints", DamageAmount);
}
}
}
}
Comment