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 Langynom · Mar 21, 2013 at 03:28 PM · raycastcolliderhitlinecast

Linecast from raycast hit point

I am currently creating a simple script for a pistol required in my college project, I have everything working apart from one of the visual effects, which when implemented causes odd behavior. What I am trying to achieve is to use a raycast to any object not in the water layer, then apply a particle effect at that point and a force if it has a rigidbody. All of this is working fine but after that I want to cast a line cast from the point the gun to the point the raycast hit and check for a water surface in-between, if there is one create a splash effect. Although this works as intended when I use this method if I shoot at a an object and there isn't water between it and the gun, the weapon fires very rapidly without depleting any ammo and I can't work out why.

The following function is called when the mouse button is pressed:

 function Fire()
 {
     if(bulletsLeft==0)
         return;
     if(Time.time - fireRate > nextFireTime)
         nextFireTime = Time.time - Time.deltaTime;
     while(nextFireTime < Time.time)
     {
         FireOneShot();
         nextFireTime += fireRate;
     }
 }

and the Fire one shot function is as follows:

 function FireOneShot()
 {
     if(audio)
     {
         audio.Play();
         audio.loop = false;
     }
     
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;    
     
     //Test for hit
     if(Physics.Raycast(transform.position, direction, hit, range, notWaterLayer))
     {
         //Apply force to any hit objects
         if(hit.rigidbody && hit.collider.gameObject.tag != "Boat")
             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
         
         //Apply Sparks
         if(hitParticles)
         {
             hitParticles.transform.position = hit.point;
             hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             hitParticles.Emit();
         }
         //Send damage to hit object
         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         
         if(Physics.Linecast(transform.position, hit.point, hit, waterLayer));
             hit.collider.SendMessageUpwards("splash", hit.point, SendMessageOptions.DontRequireReceiver);
     }
     
     //Check for water
     else if(Physics.Raycast(transform.position, direction, hit, range, waterLayer))
         hit.collider.SendMessageUpwards("splash", hit.point, SendMessageOptions.DontRequireReceiver);
     
     //Take 1 from the remaing bullets
     bulletsLeft--;
     
     //Register that the player shot this frame for muzzle effects
     m_LastFrameShot = Time.frameCount;
     enabled = true;
     
     //Reload if you run out of ammo
     if(bulletsLeft==0)
         Reload();
 }

Via comenting out various sections of the code I have found it is the Linecast itself causing the issue as when that line is removed the rest functions as I would hope it to.

Thanks, Langy

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Lockstep · Mar 21, 2013 at 04:02 PM

Remove the ; in line 29.

Also try adding curly brackets around the statement after the else if. I got the impression, that the compiler messes up with the single line statements without brackets if they are use in an else if statement when the if had brackets.

Comment
Add comment · Show 2 · Share
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 Bunny83 · Mar 21, 2013 at 04:05 PM 0
Share

Good spotted ;) That semicolon will change everything...

Curly brackets aren't necessary, but yes, they usually help to spot such mistakes.

avatar image Langynom · Mar 21, 2013 at 07:40 PM 0
Share

I cannot thank you enough, that one stray semi-colon (which I now feel incredibly stupid for not spotting) cost me a good hour of troubleshooting, fixed when I removed it, thanks again.

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

12 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 avatar image

Related Questions

Problem with LineCast Bullet 1 Answer

The Child Is Not Hovered When Passing The Parent's Collider 0 Answers

Enemy line of sight using linecast and colliders 1 Answer

Raycast2D doesn't detect objects 1 Answer

Raycast Destroy(hit.collider.gameObject); (Still need help) 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