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 /
  • Help Room /
avatar image
0
Question by MaximilianPs · Apr 22, 2016 at 04:22 PM · physicsphysics.spherecast

Get object which collide with physics.spherecast and displace to opposite direction

Hello there, from the main class, on awake I run a method which place randomly some objects on a NavMesh. Now the problem is that this object get overlapped.

To avoid the problem I wish to cast a Spherecast to check who is the guilty and move the new instanced object on the Opposite direction plus a float Gap which I wish to add to separate the object.

Here is the actual code....

 void PlaceEnergyPool()
     {
         EnergyPools.Clear();
 
         foreach (var p in Players)
         {
             for (int i = 0; i < (MaxEnergyPool / Players.Count); i++)
             {
                 GameObject energyPoolPrefab = Instantiate(EnergyPool);
                 Vector3 randomPos = GenerateNavMeshRandomPoint(NavMeshToMesh());
                 
                 float desiderDistance = energyPoolPrefab.GetComponent<NavMeshObstacle>().radius + EnergyPoolsGap;
                 if (Physics.CheckSphere(transform.position, desiderDistance, 0))
                 {
 
                     foreach (var ep in EnergyPools)
                     {
                         RaycastHit rhit;
                         if (Physics.Raycast(randomPos, ep.transform.position, out rhit))
                         {
                             if (rhit.distance <= desiderDistance)
                             {
                                 Vector3 rhitDir = ep.transform.position - randomPos;
                                 randomPos += -rhitDir;
                             }
                         }
                     }
                 }
                 energyPoolPrefab.transform.position = randomPos;
                 EnergyPools.Add(energyPoolPrefab);
             }
         }
     }

Help Help ! :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MaximilianPs · Apr 23, 2016 at 12:44 PM

What I need it to know who is inside of the sphere and in which direction, so I can move the random placed object a bit more distant... shouldn't be so hard >.<

Ok.. I've roughly made it works...

 foreach (var p in Players)
         {
             for (int i = 0; i < (MaxEnergyPool / Players.Count); i++)
             {
                 GameObject energyPoolPrefab = Instantiate(EnergyPool);
                 energyPoolPrefab.name = "EnergyPool(" + i.ToString() + ")";
                 
                 float desiderDistance = EnergyPoolsGap + (energyPoolPrefab.GetComponent<NavMeshObstacle>().radius * 2);
                 float gapX = ((MaxX / Players.Count) / MaxEnergyPool) + desiderDistance; Debug.Log("GapX = " + gapX);
 
 // This is a complex method which create a mesh = to navmesh and check triangle per triangle if the vector selected is inside the navmesh
                 Vector3 candidatePos = GenerateNavMeshRandomPoint(NavMeshToMesh());
                 Vector3 rndPos = candidatePos;
 
                 NavMeshPath path = new NavMeshPath();
                 if (!NavMesh.CalculatePath(candidatePos, p.Location.transform.position, NavMesh.AllAreas, path))
                 {
                     if (!RandomPoint(candidatePos, desiderDistance * 2, out rndPos)) // this will change rndPos
                     {
                         float temp = Random.Range(-(gapX * i), gapX * i);
                         Debug.Log("RandomPoint faild, so we use candidatePos (" + candidatePos + ") \n\t" + temp);
                         rndPos = new Vector3(candidatePos.x + temp, 0, candidatePos.z);
                     }
                 }
 
                 if (i >= 1)
                 {
                     foreach (var crater in EnergyPools)
                     {
                         // X
                         if (Mathf.Abs(candidatePos.x - crater.transform.position.x) < gapX)
                         {
                             // If the player position is near over the half of the terrain ... we need to decrease the X !
                             if (p.Location.position.x < MaxX / 2)
                                 candidatePos = new Vector3(p.Location.transform.position.x + (gapX * i), 0, rndPos.z);
                             else
                                 candidatePos = new Vector3(p.Location.transform.position.x - (gapX * i), 0, rndPos.z);
                             //Debug.Log("Player:" + p.Location.transform.position.x + ", " + energyPoolPrefab.name + ":" + candidatePos + "\n\t (desPos:" + (gapX * i) + ")");
                         }
 
                         Debug.Log("Comparing Z axis "+ crater.name +"(" + crater.transform.position.z +") with "+ candidatePos.z);
                         // Z
                         if (Mathf.Abs(candidatePos.z - crater.transform.position.z) < desiderDistance)
                         {
                             if (crater.transform.position.z < (MaxZ / 2))
                                 candidatePos = new Vector3(candidatePos.x, 0, crater.transform.position.z + desiderDistance);
                             else
                                 candidatePos = new Vector3(candidatePos.x, 0, crater.transform.position.z - desiderDistance);
                         }
                     }
                 }
 
                 if (!NavMesh.CalculatePath(p.Location.transform.position, candidatePos, NavMesh.AllAreas, path))
                     candidatePos = new Vector3(candidatePos.x, 0, p.Location.position.z);
                 
                 energyPoolPrefab.transform.position = new Vector3(candidatePos.x, 0, candidatePos.z);
                 EnergyPools.Add(energyPoolPrefab);
             }

Comment
Add comment · 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

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

74 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

Related Questions

Determining if my target is standing upright 4 Answers

RayCasting 0 Answers

Rigidbody projectile misses collider 1 Answer

Projectile Changing Angle BEFORE Impact 0 Answers

simulate a physics material bounce to find where it will land 0 Answers


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