- Home /
Limit space for a child object
Hello all,
In my scene, the cart, a childObject of the navMesh Agent, is colliding with item objects (see picture) each time the agent gets close to the shelf. I've read that I can limit the position of the object in space by using Mathf.Calmp
I've written a script to put on the cart component, but it doesn't seem to work:
void Update()
{
Vector3 clampedPosition = transform.position;
clampedPosition.x = Mathf.Clamp (transform.position.x, -1, 1);
clampedPosition.z = Mathf.Clamp (transform.positionz, -1, 1);
}
In my interpretation, this means that the cart is restricted to this borders, the borders of the parent object. alt text
Also, is it possible in Unity to restrict a game object to a baked NavMesh?
You are altering the values, but you're not writing them back to the transform. Furthermore, if you want to restrict it to whatever parent this transform has, you need to use localPosition.
Hello @hexagonius I'm writing back to transform:
private void Update()
{
Vector3 clampedPosition = transform.position;
clampedPosition.x = $$anonymous$$athf.Clamp (transform.position.x, 0.8f, 1f);
clampedPosition.z = $$anonymous$$athf.Clamp (transform.position.z,7f, 9f);
tranfrom.position = clampedPosition;
}
Yes, I would like to restrict the child to the navmesh of the scene, as the AI parent is also restricted to the navmesh of the scene you can say, that I want to restrict it to the AI parent. At what point do I use the localPosition? Thanks for the feedback, going to learn more about it.
Answer by JedBeryll · Jun 12, 2017 at 07:07 PM
Are you reassigning the clamped position to the transform somewhere else after clamping? If not, this code won't work because you are creating the clampedPosition but never using it after that. If that's the case this should work:
void Update()
{
Vector3 clampedPosition = transform.position;
clampedPosition.x = Mathf.Clamp (transform.position.x, -1, 1);
clampedPosition.z = Mathf.Clamp (transform.positionz, -1, 1);
transform.position = clampedPosition;
}
Also i'm not quite sure what your scene looks like, -1 and 1 seems a bit small, you could be clamping the position to the middle of the scene unintentionally. In that case you may need to use transform.localPosition.
As for the second part: I have no idea.
Hello, I just want to update that the cart is really seems to be "clamped" to the position, but as it's been noticed, I need to use localPosition, because it's a child object.
So I used it:
public Transform cart;
private void Update()
{
Vector3 clampedPosition = cart.localPosition;
clampedPosition.x = $$anonymous$$athf.Clamp (cart.localPosition.x, -0.38f, 2.577f);
clampedPosition.z = $$anonymous$$athf.Clamp (cart.localPosition.z, 0.08f, -3.81f);
cart.localPosition = clampedPosition;
}
But now I have the cart moving relatively to the AI position, and not the world position
So I did that ins$$anonymous$$d and it's working!
private void Update()
{
Vector3 clampedPosition = cart.position;
clampedPosition.x = $$anonymous$$athf.Clamp(cart.position.x, -1.5f, 1.5f);
clampedPosition.z = $$anonymous$$athf.Clamp(cart.position.z, 0f, 4.136f);
cart.position = clampedPosition;
}
Now I have another problem, but this is a whole new answer! I'm using an I$$anonymous$$ to have the AI "hold" the cart, and this function is stronger then I$$anonymous$$ so I find the cart drifting a side from the AI. I think to solve it with connecting both gameObjects with a hinge of some sort.
Answer by Eco-Editor · Jun 13, 2017 at 12:51 PM
Just wanted to thank you for the answers! I got it solved, by using both world location: Transform, and parent location: localTransform This way I don't need to use the hinge component.
here's the final code: public Transform cart;
private void Update()
{
Vector3 clampedPosition = cart.position;
clampedPosition.x = Mathf.Clamp(cart.position.x, -1.52f, 1.4f);
clampedPosition.z = Mathf.Clamp(cart.position.z, 0f, 4.136f);
cart.position = clampedPosition;
Vector3 localClampedPosition = cart.localPosition;
localClampedPosition.x = Mathf.Clamp(cart.localPosition.x, -0.195f, 0.064f);
localClampedPosition.z = Mathf.Clamp(cart.localPosition.z, -0.431f, -0.36f);
cart.localPosition = localClampedPosition;
}
If you think it can be improved comment below! Thanks for the answer @JedBeryll
Hello everybody
I thought this was figured out, but still the cart of the AI enters shelves, even when the position of the AI is limited to shelf "borders", as if it doesn't apply to the cart. I hoped that the clamped position will have the cart "force" the AI's position, as to avoid clipping with the shelf.
I can't use two camera's here, as it's First Person virtual reality s$$anonymous$$mVR setting, or is it possible to use two camera's and have them render on different depth?
Your answer
Follow this Question
Related Questions
How can i make child game objects interract with other objects instead of the parent ? 1 Answer
Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers
How to have a navmeshagent not move rigidbodies in my world? 0 Answers