- Home /
How do you move an object relative to a plane's global scale?
Okay, so I have a question and I've done my looking, but I haven't found any answer for my particular issue.
I have a plane, it's at a scale of 1x1x1. I also have a game object, an empty one. What I want to do is position this game object in relation to the plane's scale using code. Let's say I want the game object to sit neatly at the plane's bottom left-hand corner looking down along the Y axis. What I would like to do is math similar to this:
//please imagine this math is completed for each vector axis, otherwise I'm going to have a very wide post.
Vector3 goPosition = new Vector3 (plane.transform.position - (plane.transform.scale * 0.5))
theoretically that math should have my game object starting in the middle of the plane, then moving happily along half of the plane to the left, and then half of the plane down, reaching the corner.
the problem is, however, that the actual scale of the plane isn't relative to the world or the Vector3 Co-ordinates. so instead of my object now being at the corner of the plane, it sits slightly off-center. Is there anyway to do what I desire based on the scale of the plane? or anyway to get the plane's scale in relation to the Vector3 Co-ordinates?
Answer by Shirou14 · Oct 28, 2012 at 04:00 AM
So hey guys, I was given an answer elsewhere where instead of using plane.transform.localScale, I should instead use plane.transform.renderer.bounds.size, and it works very well.
if you guys can think up a better solution though, I'm all ears!
thanks!
Shirou
That sounds interesting. I might try it for my sort-of tennis court in my game.
Answer by swatmaster69 · Oct 28, 2012 at 04:01 AM
I believe the default size of the plane object is 10m x 10m, so you could do, say
float planeWidth = 10 * plane.scale.x;
float planeLength = 10 * plane.scale.z;
and then use those variables to move things around:
Vector3 goPosition = new Vector3(plane.position.x - (planeWidth / 2), ...
Answer by MountDoomTeam · Oct 28, 2012 at 03:12 AM
if you make the object child of the plane, then it will always stay in the same place relative to the plane it's size will also be relative to the plane size.
so you want to put your object at (-1, 0, .1)
then you do
pos= Vector3 (-1*plane.transform.localScale*.5+plane.transform.position,
plane.transform.position,
.1*plane.transform.localScale*.5+plane.transform.position)