• 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 /
  • Help Room /
avatar image
Question by Ironwolf379 · Aug 12, 2022 at 01:37 AM · raycastvector3math

How can I get the X,Z value at a height of Y along a raycast?

So I'm trying to get the X and Z position at which a raycast intersects at a certain Y level.

I'm using this in a base building system. The player is shown a preview ghost for the building section they want to place but I need the position from the raycast to position the preview ghost on the correct coordinates. I'm using some Y values as levels the player can place building sections on, so I want to use the Y values and the information from the raycast to find the point. I just don't know the math or functions to find this.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by wideeyenow_unity · Aug 12, 2022 at 01:52 AM

You're gonna need to shoot the raycast from the camera, so documentation on all that is required for this is:


https://docs.unity3d.com/ScriptReference/Physics.Raycast.html


Also screen to world is very important : https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html


and you'll probably also need to get familiar with collisions : https://docs.unity3d.com/ScriptReference/Collision-gameObject.html


Also might need to understand the Quaternions : https://docs.unity3d.com/ScriptReference/Quaternion.html


but mostly will need to know about positions : https://docs.unity3d.com/ScriptReference/Transform-position.html


and from the sounds of it, you'll need to know about colors : https://docs.unity3d.com/ScriptReference/Color.html


pretty sure there is something else, but it escapes me at the moment.. But you should be good to go :)


Ohh yeah, instantiations : https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Comment

People who like this

0 Show 3 · 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 Ironwolf379 · Aug 12, 2022 at 06:33 AM 0
Share

I have everything like that working, I'm not completely new I just need the math for the raycast is all. Currently I have it working with detecting a collision with a certain layermask and checking if the object is a building area collider. But I want to remove having the colliders so I can have multiple building levels without having to check for which collider its hitting and so on. It all functions fine I just need help with the math but thanks anyway.

avatar image wideeyenow_unity Ironwolf379 · Aug 12, 2022 at 03:37 PM 0
Share

In the documentation on raycast, you should have noticed :

 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))

The hit is what you're looking for, the hit.position to be exact. So not sure if you need a check of when hit.position.y is where you need it, or if you only want it to return if hit.position.y is where it should check?


I'm not 100% on understanding exactly how you wish to implement it. (sorry)

avatar image Ironwolf379 wideeyenow_unity · Aug 15, 2022 at 11:26 PM 0
Share

So I basically just want to shoot a raycast out anywhere from the camera and get the point at which it passes a set Y value. Without needing to use a collider to get it.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Find a point in space using Vector3 angles and Raycast 1 Answer

How to calculate next camera position to look at the object that changed rotation 1 Answer

How do I convert speed and heading to a vector? 3 Answers

I'm trying to find something out about variables. Please help! 1 Answer

How to detect when an object is within an arc? 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