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 MihaiC · Jul 19, 2014 at 07:51 PM · raycastterrainrandomspawning

How to get the y coordinate of a point based on the x and z coordinates

So basically I am working on a game at the moment and I have a random object spawner which spawns the object randomly on the plane, this worked before as I had a simple plane set at a y of 3, now I received a new plane .obj which is sloped and has different y values at different points, and so the spawner does not work any more. My question is how can I get the y position of a point based on the x and z coordinates and based on the returned y position spawn the object. I was thinking of raycasting but from what I can tell it does not return the Y value when it hits something. Is there any way to do this or would I have to change the terrain and set a specified Y pos for each section of the terrain.

Thank you.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jul 19, 2014 at 07:53 PM

What's wrong with raycast? The point property of the raycasthit gives you a Vector 3 in world space, so you can take the y coordinate straight from that: http://docs.unity3d.com/ScriptReference/RaycastHit-point.html

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
0

Answer by rutter · Jul 19, 2014 at 08:00 PM

If you think about it abstractly, an x-coordinate specifies "some distance to the right", and a z-coordinate specifies "some distance forward".

So this:

 Vector3 v = new Vector3(x, 0, z);

Is the same as:

 Vector3 v = Vector3.right * x + Vector3.forward * z;

The above uses "world space". Every object in Unity has a transform component, which keeps track of its position, rotation, and scale. The transform also allows you to convert between world space and "local space" (relative to that specific transform).

So you could use something like this:

 Vector3 v = transform.TransformPoint(x, 0, z);

Or:

 Vector3 v = transform.position + transform.right * x + transform.forward * z;

All of this assumes that the transform's opinion of "right" and "forward" agrees with what you're expecting. If they don't, it's simple enough to switch around the axes:

 Vector3 v1 = new Vector3(0, x, z); //for example
 Vector3 v2 = transform.TransformPoint(v1);

You'll also need a reference to the plane's very own transform. Maybe that's the same transform your script is attached to, or maybe you need to set up a reference in the inspector. There are plenty of tutorials to help with that bit, if you need.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiated Objects not being set at ground/terrain level?(Solved) 1 Answer

Building placement validation on curved terrain 0 Answers

using Raycast.hit function my car gets up and doesnt gets down ? it is in flying state 1 Answer

Flattening terrain under my object at start() 1 Answer

Have object follow mouse on terrain 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