• 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 /
This question was closed yesterday by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by NiceAustralian · 4 days ago · raycastcollision detection

How to detect when an object is exactly above another object?

For example when a default cube (1 x 1) is directly above another default cube (1 x 1).

It is important that the cube is exactly above it.


I have a cube moving in a direction, when it comes directly over another cube with an arrow it changes direction, so I need to detect when the cube is directly above it, then run the code to change the direction.


If it is out a bit it doesn't stay moving along a grid, so Enter / Exit collision checks don't work as far as I know.

Checking the distance doesn't seem to work because of floating point imprecision.

Physics.RaycastAll has been suggested but I don't know how to get that to 'know' that the cube is directly above another.


Hopefully this makes sense, any advice is appreciated.

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

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by tormentoarmagedoom · 3 days ago

Hello.

You have different ways to do it.

I would use Raycast or colliders to first detect top nearby objects. Then calculate the distances, but not looking for the exxact distance.

1- Detect nearby objects with colliders and OnTriggerStay

2- Calculate the Vector3 position point of what i consider tha exact "above point"

3- Calculate the distance between nearby object position and above point poistion

4- If this Vector3.distance is lower than 0.1f (for example) i consider is above.

Bye!!

Comment
Add comment · Show 1 · 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 NiceAustralian · 3 days ago 1
Share

I eventually experimented with something like this, but used a number like 0.001f, but I think its not noticeable at all when its that small of a number. This worked for me.

avatar image
1

Answer by molydoly · 3 days ago

Hello, You can find this in the following ways. If you want to check if 'any' object is near you could do two things. Either use a trigger to see when it becomes near and save a reference to this object or use Physics. OverlapSphere to find all objects within a certain range.

Comment
Add comment · 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
1

Answer by Venividiviciuus · 3 days ago

The best solution for your problem is a Raycast. Plus I recommend that it would be better if you have more than one raycast for check the result e.g. one from the center and two from the sides of the first object

 RaycastHit hit;
 if(Physics.Raycast(cube.position, Vector.up, out hit,  0.1f))
 {
        if(hit.point == gameObject.tag("Tag"))
             Debug.Log("Above");
 
 }
 
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 NiceAustralian · 3 days ago 0
Share

I will try this way out, never thought of using multiple raycasts in this way.

avatar image Venividiviciuus NiceAustralian · 3 days ago 0
Share

In the end it really depends on what you need to do, without a clear picture it is very likely that what you want to achieve is not exactly what my solution is. This type of function it is very useful if you want to check if there is something on your object, it works for, you while crouching and wanting to stand up is not possible if there is something above it. For this reason, before using my solution, give us an image of your objects showing the behavior you would like to adopt. So we will know better how to address you.

Using 3 Raycasts as I suggested, and a condition that all three values are true you can determine if there is actually another 1x1 cube above your 1x1 cube.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Jarring "snap" on 2d Camera raycast collider 0 Answers

Raycast passes Collider 0 Answers

AI bug issue 0 Answers

Is it possible to form a parabolic arc with a Ray using a VVF? 1 Answer

Raycast goes through collider 2 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