Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Eco-Editor · May 09, 2017 at 07:45 AM · collisiongameobjectvrnavigationchild object

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?

cart-in-the-stand.jpg (52.4 kB)
Comment
Add comment · Show 2
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 hexagonius · Jun 11, 2017 at 09:10 AM 0
Share

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.

avatar image Eco-Editor hexagonius · Jun 12, 2017 at 06:38 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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 Eco-Editor · Jun 12, 2017 at 08:05 PM 0
Share

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.

avatar image
0

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

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 Eco-Editor · Jun 19, 2017 at 03:43 PM 0
Share

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

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

129 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

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

How to stop Camera from going into colliders 3 Answers

Create A Counter And GameObjects Unique ID 1 Answer


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