Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xthunderduckx · May 29, 2019 at 07:54 PM · transformraycastingsendmessagereceiversender

sendMessage doesn't work on objects while the object is behind the sender's transform

Edit: I've made a revalation.
It turns out that the raycast will actually work properly if the edge that the ray is supposed to hit is in view of the camera. Unfortunately, that isn't always something that can be seen, and for whatever reason, when that edge is not visible, the raycast doesn't collide or something, and it won't hit as a result. Is there any way to ensure that the raycast hits an unrendered side of an object?

alt text

As shown in the above image, I have a number of cubes. Each of the five cubes has a method called "takeDamage", and an int as an argument. The player has a weapon which fires a projectile, and onCollisionEnter, it overlaps a sphere to check if objects of type "Enemy" are in a radius. After it finds all the objects in that sphere, it raycasts towards them in order to make sure objects which don't register as "enemy" are in the way of the explosion.


What is puzzling to me about why this isn't working is that the objects directly behind the projectile's contact point receive the relevant error, the two red highlights representing the cubes behind the projectile. I tried rotating around 90 degrees and instead it didn't hit the two cubes on the right. If I simply sendMessage takeDamage with the overlapSphere, it works fine.


Here is my code, maybe somebody knows what I'm doing wrong and can help me fix my error.

 private void OnCollisionEnter(Collision collision)
     {
         grenadeBody.constraints = RigidbodyConstraints.FreezeAll;
 
         if (collision.gameObject.tag == "Enemy")
         {
             collision.gameObject.SendMessage("takeDamage", 125);
         }
         //if grenade directly hits enemy, deals additional damage. 
         Collider[] collisionPoints = Physics.OverlapSphere(transform.position, 1.5f, overlapLayer);
         //bool targetReached = false;
         for(int i = 0; i < collisionPoints.Length; i++)
         {
             Vector3 rayPos = transform.position;
             Vector3 dir = collisionPoints[i].transform.position - transform.position;
             Debug.DrawRay(rayPos, dir, Color.cyan, 10f);
             if (Physics.Raycast(rayPos, dir, out RaycastHit explosiveHit))
             {
                 explosiveHit.collider.SendMessage("takeDamage", 225);
             }
         }
         //somehow, sendMessage(takeDamage, int) doesn't work on objects which are behind the transform.position.  
         Destroy(this.gameObject);
     }


testEnemy.cs (the receiver):

     int health;
     // Start is called before the first frame update
     void Start()
     {
         health = 1000;
     }
     private void takeDamage(int damage)
     {
         health -= damage;
         if (health <= 0)
         {
             print("Enemy died.  Health value:  " + health);
             Destroy(this.gameObject);
         }
     }
 }

Edit: I don't know if I mentioned this but overlapSphere and then sendMessage(takeDamage) to each collider works perfectly, but I need the raycasts. So why is it that the sphere detects everything properly, and can send messages fine, but the second a raycast is involved, it works against whatever is in front of it but not what is behind it?

noreceiver.png (153.0 kB)
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

142 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 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 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 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 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 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 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 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 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

Raycast2D and origin position Offset 1 Answer

How To Move Object To Terrain Slope in C# With Raycast? 2 Answers

Using raycast, find another object, send message. 1 Answer

Message not being recieved 1 Answer

How do i get the transform of an object with raycasthit? 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