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 MegaM3tin · Nov 29, 2012 at 02:45 PM · cameraraycastfpsshooting

raycast spawn problem

Hello, I'm working on a game project in school with a couple of classmates. I'm making a gun script for our FPS shooter and I have encountered a problem. The problem is when I'm shooting my raycast casting points get's further and further down for every shot and I don't know why. The raypoint don't move (The object where I start my raycast from). Any one that has any idea what the problem may be? I have pasted my code in the post.

Thanks in advance.

  @HideInInspector
     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 racioHipHold : float = 1;
     var hipToAimSpeed : float = 0.1;
     @HideInInspector
     var racioHipHoldV : float;
     
     var aimRacio : float = 0.2;
     var zoomAngle : float = 40;
     
     var fireSpeed : float = 1;
     @HideInInspector
     var waitTilNextFire : float = 0;
     
     var recoilAmount : float = 0.2;
     var recoilRecoverTime : float = 0.2;
     @HideInInspector
     var currentRecoilZPos : float;
     @HideInInspector
     var currentRecoilZPosV : float;
     
     var floatInFrontOfWall : float = 000.1;
     
     var gunbobAmountX : float = 0.5;
     var gunbobAmountY : float = 0.5;
     var currentGunbobX : float;
     var currentGunbobY : float;
     
     static var AMMO : int = 50;
     
     var clickSound : AudioClip;
     var gunSound : AudioClip;
     
     var decalHitWall : GameObject;
     var muzzleFlash : GameObject;
     var raySpawn : GameObject;
     
     
     var range : float = 300;
     
     function Start() {
        ammo = 50;
     }
     
     
     
     function LateUpdate () {
         
         currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * racioHipHold;
         currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * -1 * racioHipHold;
         
     
         if(AMMO > 0) {
         
             if (Input.GetKeyDown(KeyCode.Mouse0)) {
         
                 if (waitTilNextFire <= 0) {
                 
                     RayShoot();
                         
                     PlayShootAudio();
                         
                     var clone : GameObject;
                     clone = Instantiate(muzzleFlash, raySpawn.transform.position, transform.rotation);
                         
                     currentRecoilZPos -= recoilAmount;
                     
                     waitTilNextFire = 1;
                     
             }
         }
      }    
      
      if (AMMO <=0 ) {
         
         if (Input.GetKey(KeyCode.Mouse0)) {
         
         if (waitTilNextFire <= 0) {
         
                 PlayClickSound();
                 
                 waitTilNextFire = 1;
                 
             }
             
         }
         
     }
         
         waitTilNextFire -= Time.deltaTime * fireSpeed;
     
         currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
     
         cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
     
         if (Input.GetKey(KeyCode.Mouse1)) {
         
             cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
             racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);
             
             }
             
         if (Input.GetKey(KeyCode.Mouse1) == false) {
         
             cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
             racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);
             
             }
     
         transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold + currentGunbobX, holdHeight * racioHipHold + currentGunbobY, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
         
         targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
         targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
         
         transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
         
         
     }
     
     function RayShoot() {
     
           AMMO -= 1;
         
         var hit:RaycastHit;
         
         var directionRay = raySpawn.transform.TransformDirection(Vector3.forward);
     
         Debug.DrawRay(raySpawn.transform.position, directionRay * range, Color.blue);
     
         if(Physics.Raycast(raySpawn.transform.position, directionRay, hit, range)) {
             
             if(decalHitWall && hit.transform.tag == "Level Parts") {
                 Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
                 
                 
         }
     
       }
       
     }
     
     function PlayShootAudio() {
         audio.PlayOneShot(gunSound);
     
     }
     
     function PlayClickSound() {
         audio.PlayOneShot(clickSound);
     
     }
 
Comment
Add comment
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

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Raycasr in my fps? 1 Answer

Straight Raycast Shooting 1 Answer

One animation for two objects 0 Answers

Raycast shooting in the middle of the screen 1 Answer


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