• 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
Question by artman_12 · Feb 05, 2013 at 12:16 PM · physicsrigidbodypush

Push object away from another object

Hi all. I have a moving sphere with a rigidbody on it. At some event I destroy this sphere and instantiate 2 smaller spheres.

 GameObject pop1 = Instantiate (spherePrefab, new Vector3 (parentPos.x, 0 , parentPos.z), transform.rotation) as GameObject;
 pop1.rigidbody.velocity = parentVel;
             
 GameObject pop2 = Instantiate (spherePrefab, new Vector3 (parentPos.x, 0 , parentPos.z), transform.rotation) as GameObject;
 pop2.rigidbody.velocity = parentVel;

These 2 spheres are instantiated in the same place, and as they are rigidbodies, they show up one aside another and they have the same velocity as parent sphere.

alt text

Could you please tell me how can I make 2 smaller sphere move away from each other as well, as shown on the second picture?

alt text

1.png (3.8 kB)
2.png (4.1 kB)
Comment

People who like this

0 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 Lockstep · Feb 05, 2013 at 02:19 PM 0
Share

Check out the docs.

avatar image artman_12 · Feb 05, 2013 at 02:30 PM 0
Share

thanks, but I know about this method. The problem is much deeper. I'll write an answer right now.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by artman_12 · Feb 05, 2013 at 03:01 PM

The problem is much deeper, than it seems in the first place. I'm instantiating 2 smaller spheres in the same position as the parent sphere. This is my main mistake. I should instantiate them a bit to the sides. to calculate this offset I turned velocity vector to -90 degrees, normalized it, multiplied by sphere radius and cached this value. It would be used for 1st sphere I've done the same for another sphere, but the radius was 180, instead of -90. thus I've got 2 points in the world, where I can instantiate 2 smaller spheres.

     Vector3 vel = parentSphere.rigidbody.velocity;
     Vector3 newVel1 = Quaternion.AngleAxis(-90, Vector3.up) * vel;
     Vector3 offset1 = newVel1.normalized * parentSphereRadius;
 
     Vector3 newVel2 = Quaternion.AngleAxis(180, Vector3.up) * vel;
     Vector3 offset2 = newVel1.normalized * parentSphereRadius;


Now I have to calculate vector to push my spheres apart from each other.

     GameObject pop1 = Instantiate (spherePrefab, new Vector3 (parentPos.x, 0 , parentPos.z), transform.rotation) as GameObject;
     Vector3 pop1Delta = parentPos - pop1.transform.position;
     pop1.rigidbody.velocity = parentVel;
     pop1.rigidbody.AddForce(pop1Delta);
      
     GameObject pop2 = Instantiate (spherePrefab, new Vector3 (parentPos.x, 0 , parentPos.z), transform.rotation) as GameObject;
     Vector3 pop2Delta = parentPos - pop2.transform.position;
     pop2.rigidbody.velocity = parentVel;
     pop1.rigidbody.AddForce(pop2Delta);


That's it! Now it works correctly.

Comment
joshbowden42
jacobrutter

People who like this

2 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

How to set Player to push only during pushing animation 1 Answer

Cube collision problems when held 0 Answers

Player Pushes Enemies. (That's bad) -Still Unanswered- 1 Answer

Rigidbody triggers are pushing a CharacterController.. 3 Answers

Amnesia like objects, in unity. 2 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