- Home /
Using Raycast to shoot a bullet sometimes goes through objects
ok so im shoting a bullet with a ray cast my GunScript instantiates the bullet then I have a script atached to the bullet that shoots it using raycast. The only problem is when I shoot it sometimes ads the bullet hole to the object in front of me sometimes it goes through.
Here is my bullet script:
var maxDist : float = 1000000000;
var decalHitWall : GameObject;
var floatInFrontOfWall : float = 0.001;
function Update ()
{
var hit : RaycastHit;
if(Physics.Raycast (transform.position, transform.forward, hit, maxDist))
{
if(decalHitWall && hit.transform)
Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
Destroy(gameObject);
}
and here is my gun script:
var cameraObject : GameObject;
@HideInInspector
var targetXRotation : float;
@HideInInspector
var targetYRotation : float;
@HideInInspector
var targetXRotationV : float;
@HideInInspector
var targetYRotationV : float;
var rotateSpeed : float = 0.3;
var holdHeight : float = -0.5;
var holdSide : float = 0.5;
var ratioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var ratioHipHoldV : float;
var aimRatio : float = 0.4;
var zoomAngle : float = 30;
var fireSpeed : float = 15;
@HideInInspector
var waitTilNextFire : float = 0;
var bullet : GameObject;
var bulletSpawn : GameObject;
var shootAngleRandomizationAiming : float = 5;
var shootAngleRandomizationNotAiming : float = 15;
var recoilAmount : float = 0.5;
var recoilRecoverTime : float = 0.2;
@HideInInspector
var currentRecoilZPos : float;
@HideInInspector
var currentRecoilZPosV : float;
var bulletSound : GameObject;
var muzzleFlash : GameObject;
var gunbobAmountX : float = 0.5;
var gunbobAmountY : float = 0.5;
var currentGunbobX : float;
var currentGunbobY : float;
function LateUpdate ()
{
currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLook).headbobStepCounter) * gunbobAmountX * ratioHipHold;
currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLook).headbobStepCounter * 2) * gunbobAmountY * ratioHipHold;
var holdMuzzleFlash : GameObject;
var holdSound : GameObject;
if(Input.GetButton("Fire1"))
{
if(waitTilNextFire <= 0)
{
if(bullet)
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if(bulletSound)
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if(muzzleFlash)
holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
if(holdSound)
holdSound.transform.parent =transform;
if(holdMuzzleFlash)
holdMuzzleFlash.transform.parent =transform;
currentRecoilZPos = Mathf.SmoothDamp(currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
cameraObject.GetComponent(MouseLook).currentTargetCameraAngle = zoomAngle;
if(Input.GetButton("Fire2")){
cameraObject.GetComponent(MouseLook).currentAimRatio = aimRatio;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);}
if(Input.GetButton("Fire2") == false){
cameraObject.GetComponent(MouseLook).currentAimRatio = 1;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);}
transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * Vector3(holdSide * ratioHipHold + currentGunbobX, holdHeight * ratioHipHold + currentGunbobY, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0, 0, currentRecoilZPos));
targetXRotation = Mathf.SmoothDamp(targetXRotation, cameraObject.GetComponent(MouseLook).xRotation, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation, cameraObject.GetComponent(MouseLook).yRotation, targetYRotationV, rotateSpeed);
transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}
What am I doing wrong?
this should work, are you sure it's not doing what it's supposed to do?
Yes I'm sure thats not what its supposed to do 75% of the time it hits the wall or box the rest of the time it goes through and will go through and hit something behind it
Your answer
![](https://koobas.hobune.stream/wayback/20220613094241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet collision for space shooter - colliders, rays, etc? 2 Answers
problem with touch android 0 Answers
Physics.Raycast Interval problem in the FixedUpdate(). 0 Answers
What is this variable type? 2 Answers
Problem with raycast 0 Answers