- Home /
Objects with same position appear in different places
Hi,
I'm having problems comparing location of two different objects. I have a ball and a basket hoop, and I want to calculate where they are located relative to each other, so I try comparing the "transform.position" of both objects, but get incorrect results.
According to documentation, position relates to world space and not local space, so I assumed that is absolute position. Then I saw my ball moving down instead of towards the hoop, and upon inspection found that the hoop's "y" position appears smaller than the ball's, even though it is obviously higher in the graphic display.
Then I tried setting them both to (0,0,0), but they still appear in different locations visually.
So I assume that my understand of "world space" is incorrect, but I couldn't find any other property in the API or in the inspector that would tell me the absolute location of the objects in my scene.
Any help understanding these concepts would be appreciated, Google was not able to help...
Edit: The scene was created by a friend imported from Maya.
Edit2: It appears that the hoop's center/pivot (not sure which) is in a different location than the object's mesh appears in. Why is that?
Answer by Kryptos · Jul 30, 2012 at 10:31 AM
Possibly the center of both object is not correctly "centered".
Check that the scene gizmo of the transform is positionned at the center on each object. If not put the wrong-positionned object under another GameObject (parenting) and correct the local position so that the object is centered relative to its parent (i.e. when selecting the parent, the scene gizmo appears at the center of the object).
Ok, I think this was it. In the GUI I can select "Center" or "Pivot". When I select "Pivot" I can see that the tool is in the same position for both objects even though the object's mesh appears in a different position...
So I guess this has something to do with how unity imported the objects from $$anonymous$$aya?
I'll add a screenshot to my question.
Answer by Bunny83 · Jul 29, 2012 at 04:54 PM
Generally Unity works with local space coordinates (localPosition). In the inspector you will always see the localPosition and localEulerAngles.
.position will let you access it's world position. This is just a wrapper property. When you set position of an object that is a child of another object, Unity will calculate the correct local position the object needs to appear at the given world position.
Same thing for rotation / eulerAngles.
Still - from my script I checked the ".position" and it showed the same numbers as the inspector.
I tried calculating the difference between the ".position" of the hoop and the ball, and my calculations ended up with the ball going the opposite direction... (of course I subtracted the ball's position from that of the hoop, not the other way around)
Any ideas why?
We have still no idea what you're actually doing. how do they move in different directions? how do you move them? Are the objects rotated? Are you sure both objects don't have a parent object?
$$anonymous$$aybe you can post the code you use for the movement and maybe a screnshot which shows your described behaviour.
I'm trying to give throw the ball towards the hoop when the player presses a button.
Here's a code snippet (C#) and I added a screenshot to my post:
void Update () { if (Input.GetButtonDown("Jump")) { Transform hoop = GameObject.Find("Basket_Hoop").transform; float[] hoopPosition = vectorToArray(hoop.position); float[] ballPosition = vectorToArray(transform.position);
float[] velocity = vectorToArray(rigidbody.velocity); for (int i = 0; i < 3; i++) { if (i == 1) { velocity[i] += speed; continue; } velocity[i] = (hoopPosition[i] - ballPosition[i]); }
Debug.Log("altitude:" + transform.position.y); rigidbody.velocity = arrayToVector(velocity); } }
Your code is very strange. Why do you convert the vectors into float arrays? It's much easier this way:
Transform hoop;
void Start()
{
hoop = GameObject.Find("Basket_Hoop").transform;
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
Vector3 newVelocity = hoop.position - transform.position;
newVelocity.y = rigidbody.velocity.y + speed;
rigidbody.velocity = newVelocity;
}
}
But your real problam is probably that your hoop objects pivot is somewhere at the base plate, and not at the hoop position. Select your hoop object, make sure the pivotmode is set to pivot and not to center.
then you will see where the pivot of that object is. This position is the world position of that object.
As solution you could add an empty gameobject as child to your hoop object at the desired position and then use this object for your calculations.
Thanks, I asked my friend that created the models and he made new ones with correct pivot points...
BTW - I'm completely new to C# and unity so not familiar with all the APIs... thanks for the tip about subtracting vectors.
Your answer
Follow this Question
Related Questions
Moving object with transform.position ignore other objects even if they collided 1 Answer
Need enemy to follow only active character 0 Answers
Drawing a (debug) line between anchored UI element and mouse? 0 Answers
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers