Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Courtneyssg · Dec 11, 2019 at 10:48 PM · 3dtransform.positionlocalscalelocalpositionworld coordinates

How to get max and min points of a game object on the x and z axis?

Basically I have a 3D platform my player can walk across, however I don't want the player to walk over the edge so I'm trying to find the edges of my platform on the x and z axis so that I know the position at which my player has to stop moving without actually hard-coding in the player position when it encounters the edge.

I figured something like getting the x coordinate of the position of the platform and then adding half it's local scale to it to get the edge in the positive x direction would work. So if I were to do this:

     float _maxXPos = _terrain.transform.position.x + _terrain.transform.localScale.x / 2
     if(transform.position.x >= _maxXPos)
     {
         transform.position.x = new Vector3(_maxXPos, transform.position.y, transform.position.z);
     }

I was hoping that if the player reached _MaxXPos it would stop moving, however the value it stops at is much smaller. If I use the code above and use the following values:

 Terrain Position = 0, -2.57, 0
 LocalScale = 0.8, 0.8, 0.8

When player is at the maximum edge on the x axis the value is 2.1f. If I hard code this in, in place of _MaxXPos the player stops where I want it to, however if I use _maxXPos

 0 + 0.8 / 2 = 0.4 

the player stops when it reaches 0.4f on the x axis.

I have a feeling this has something to do with converting world and local coordinates but I can't seem to figure it out. Hoping someone could shed some light on this for me. Thanks!

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Smurfj3 · Dec 13, 2019 at 11:38 AM

I can confirm your code is correct because I use that myself in my game. The only things you have to make sure of though is that the object is not rotated in any way and I personally do it on a gameobject and not my terrain, maybe the terrains transform.position is not the middle of the terrain but 1 of the corners? Are you also sure you have the right terrain?

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 Courtneyssg · Dec 13, 2019 at 09:25 PM 0
Share

YES! The transform.position was the middle but my friend had put in the terrain rotated, it works now, thanks!

avatar image
0

Answer by oTaijjo · Dec 13, 2019 at 08:32 AM

Hi, I would also suspect local/world issues here. I would also advice against using scale to determine positions. You could alternatively check _terrain.GetBounds() to retrieve the terrain's Bounds. With those you might get more reliable data on the size of the terrain.

Since Bounds are in world space you'd have to transform the data into whatever space your player is moved in via the transform methods (e.g. Transform.TransformPoint(), Transform.InverseTransform()). Note that the player might move inside it's parent's space, and the parent transform might have to do the transforming NOT the player transform.

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 YiYiBaBa · Dec 13, 2019 at 09:54 AM

I think you can try to add a collider, build the boundary with Cube yourself, and hide the Mesh so you don't have to calculate it yourself.

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

153 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

Related Questions

World coordinates conversion to cell coordinates in tilemap 1 Answer

I generated many spheres and unity is so slow and laggy. What should I do? 2 Answers

Attempting to Calculate and Change Scale of Platforms Sets Scale to 0 1 Answer

Locals How to get rid of them can I disable them??? 1 Answer

Problem with transform.position on unity 3D,Transform.position not working as expected unity3D 0 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