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 AlejandroGorgal · Nov 28, 2013 at 07:19 PM · collisionraycastjavablock

How to block a raycast? (Java)

Hi, Im trying to implement a shield system for my project, the idea is that if the shield is being held up then it'll block any incoming attacks.

Now, when the enemy is in range it'll use a Raycast function to see if the player is in range and send a notification to damage him, here's the code that makes that work:

 var hit : RaycastHit;
     if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) 
     {
         Distance = hit.distance; 
         // Checks if the raycasted object is in the range of attack
         if (Distance < AttackRange) 
         {
             // Calls the function "Apply Damage" on the object that's being hit
             hit.transform.SendMessage("ApplyDammage", attackDMG, SendMessageOptions.DontRequireReceiver);
         }
     }    

What Im wondering is if there's a way to effectively block this raycast if an object such as the shield is in the way. I tried giving the shield a collision box to see if that would stop the damage notification from reaching the player but that didnt work.

Is there any way to make that work? I know that I can probably make a condition to stop the player from receiving damage when the shield is up but I still want him to get damaged if he's hit on the back.

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
1
Best Answer

Answer by robertbu · Nov 28, 2013 at 08:29 PM

There are a number of ways of solving your problem. Which one to use will depend on your game:

1) Turn off the collider on your player when the shields are up.

2) Change the name or the tag on the player when the shields go up and check the name or tag in your raycast:

 if (hit.collider.tag == "Player" && Distance < AttachRange) {


3) Use a child game object with a collider larger than your player. Enable the collider when the shields go up. Check the name or tag as in #3.

4) Use GetComponent() to get the player script and check the shields variable directly:

 var player : PlayerScript = hit.collider.GetComponent(PlayerScript);
 if (player != null && !player.shieldsUp  && Distance < AttachRange) {
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 AlejandroGorgal · Nov 28, 2013 at 08:48 PM 0
Share

Thanks for the reply, I think any of those methods could work, but the problem is that they will block attacks co$$anonymous$$g from all directions, as I explained on my first comment I still want enemies to be able to attack the player from the back even if the shield is up.

I was hoping that if I add a collider to the shield then it would block the raycast co$$anonymous$$g from the enemy, but apparently it goes right through it and still hits the player.

Is there no way to block raycast co$$anonymous$$g from one direction only? From your examples it seems like it's all or nothing.

avatar image AlejandroGorgal · Nov 28, 2013 at 09:23 PM 0
Share

Actually, I've been experimenting and this does work, any object with a collider WILL block a raycast, it's just that the shield was a child of my character so the raycast was hitting him first. Im going to try to make it so when you press the shield button it enables an invisible collider in front of the player, as long as it is above the player's hierarchy this should work.

Thanks for the help though :)

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

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

Related Questions

Unreliability of Physics.Raycast.Distance for player movement? 0 Answers

How To: Spawn object on raycast collision. 2 Answers

Destroying all objects with a certain tag that ray cast if colliding with 0 Answers

Receiving information about getting hit by a Raycast 2 Answers

Surface with hole and Raycast - Which collider 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