- Home /
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!
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?
YES! The transform.position was the middle but my friend had put in the terrain rotated, it works now, thanks!
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.
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.
Your answer
Follow this Question
Related Questions
World coordinates conversion to cell coordinates in tilemap 1 Answer
Why transform.TransformPoint(transform.position) prints differently than single transform.position? 1 Answer
Attempting to Calculate and Change Scale of Platforms Sets Scale to 0 1 Answer
Can I use the local x,y transforms of a plane? 1 Answer
I generated many spheres and unity is so slow and laggy. What should I do? 2 Answers