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 /
This question was closed Apr 10, 2017 at 11:07 AM by dashticle for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by dashticle · Apr 10, 2017 at 12:40 PM · rigidbodycolliderstransform.positionrigidbody.velocitypooling

Setting transform.position makes object appear in wrong place sometimes. Possible conflict with colliders/rigidbody?

Hi all, been trying to findwhat is going on here but no luck so far, and searching through a lot of Answers/forums. Hopefully someone can shed some light.

I'm making a simulator where AI Agents wander around a landscape, picking up Food objects (just spheres with a trigger and script attached, spawned at random locations on the navmesh). The Agents also have the ability to 'throw' Food objects to each other (i.e. they Spawn a Food object at their location, and add velocity to it towards it's recipient.)

My problem: sometimes, (like maybe once every 30ish times) the Food object they 'throw' does not Spawn in the agent, instead it appears in a random location on the navmesh, with seemingly the correct velocity.

The Food objects are instantiated and pooled at the Start of the simulation, and enabled as needed by a Food Manager, like so:

FoodManager.cs:

     for(int i = 0; i < foodPool; i++)
     {
         GameObject food = Instantiate(prefabAgent);
         food.transform.parent = gameObject.transform; 
         foodScript = food.GetComponent<Food>();
         foodScript.foodManager = this;
         food.SetActive(false);
         foodList.Add(food);
     }

And spawned like so (also in FoodManager.cs):

 public GameObject spawnFood(Vector3 position)
 {
     //use first inactive object in pool
     for (int i = 0; i < foodList.Count; i++)
     {
         if (!foodList[i].activeInHierarchy)
         {
             foodList[i].transform.position = position;
             foodList[i].SetActive(true);
             spawnedFoodObjects.Add(foodList[i]);
             allFood.Add(foodList[i]);
             //break;
             return foodList[i];
         }
     }
     Debug.Log("No inactive food found in pool, FoodManager.spawnFood() returning null");
     return null;

 }

The agents are Tossing the food with this method (most of this is setting up the values in the Food script, the relevant call is at the start. The added Vector is just to make the food 'float' a little off the terrain, to match the randomly distributed food.) In StatePatternAgent.cs:

 public GameObject TossFood(GameObject recipient)
 {
     GameObject foodObject = foodManager.spawnFood(transform.position + new Vector3(0,0.3f,0));
     //Collider[] collidersAtSpawn = Physics.OverlapSphere(position, 1);

         Food foodScript = foodObject.GetComponent<Food>();
     foodScript.value = Mathf.Clamp((hunger - stash)/(100 / (altruism+1)), 5, 25); //placeholder to avoid divide by 0, change
     foodScript.recipient = recipient.GetComponent<StatePatternAgent>();
     if (foodScript.value > (hunger - foodScript.recipient.hunger) / 2) //cap the value to be maximum the difference between the 2 agents
         foodScript.value = (hunger - foodScript.value) / 2;        
     foodScript.tosser = this;
     foodScript.tosserName = agentName;

     Vector3 recipientLocation = recipient.transform.position;
     Vector3 direction = recipientLocation - this.transform.position;
     foodObject.GetComponent<Rigidbody>().velocity = (direction + recipient.GetComponent<NavMeshAgent>().velocity) * 2;
     recipient.GetComponent<StatePatternAgent>().wanderState.chosenFood = foodObject; //try make the recieving Agent go get this one
     foodManager.allFood.Add(foodObject);
     return foodObject;
 }


(I can't seem to upload images from inspector right now like I would want, i'll try again later.) The Food object is a simple sphere, with sphere collider and rigidbody attached. The Agent has a sphere collider, rigidbody and NavMesh Agent.

I am sure this problem is somehow creeping in in the Vector3 paramter passed to the spawnFood method in FoodManager. Thigs I have tried so far: setting all the prefab transforms to 0,0,0 and resetting their scales to 1. Tried not parenting the Food objects to the FoodManager transform.

Also tried Spawning the Food objects outside the Agent directly, as I heard that trying to move objects inside another collider could have this problem. This did not seem to work, but maybe a more complex check to spawn the Food somewhere not occupied by anything else? What confuses me though is if this is the issue, it should happen ALL the time, as at the moment the Food object is being moved into the throwing Agent , enabled, and then having velocity added.

Can anybody tell me what is happening here? If I bite the bullet and add a fairly complex check to make sure the Fod is enabled somewhere unoccupied nearby the throwing Agent, will this solve my woes, or is something else going on here?

Thanks for any help and please let me know if I can clarify anything, I'm quite new to this and I'm sure my code/explanation leaves a lot to be desired.

Comment
Add comment · Show 1
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 dashticle · Apr 09, 2017 at 03:36 PM 0
Share

Sooo... it turned out everything was working.

I just wasn't zeroing the Velocity of the Food objects when the Food$$anonymous$$anager pooling was reusing them for 'static' spawns, so they were appearing on the landscape and then flying off as if thrown by the last Agent to have thrown it.

Adding

foodList[i].transform.GetComponent().velocity = Vector3.zero;

to the spawnFood method in Food$$anonymous$$anager worked.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

rigidbody.velocity or transform.position 1 Answer

Can't set the transform.position of imported objects correctly 0 Answers

VR Rigidbody not working with child colliders 0 Answers

How to Dash smoothly and Jump fall faster with a 3D rigidbody? 0 Answers

Moving rigidbody (Player) with addForce or Velocity ? 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