Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dylloop · Mar 12, 2013 at 03:16 AM · raycastbulletnot colliding

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Mar 12, 2013 at 11:39 PM 0
Share

this should work, are you sure it's not doing what it's supposed to do?

avatar image dylloop · Mar 12, 2013 at 11:48 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges