Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Oliver1135 · Dec 26, 2014 at 03:04 PM · rigidbody2dphysics2dwall collision

Unity2d Set Different Physics Properties on Gameobjects (potentially at Runtime)

I currently have two gameobjects of the same prefab interacting with physics but cannot get the right settings to make it do what I want.

alt text alt text What I have is:

When I make the red controllable gameobject move into the blue character it nudges the blue gameobject.

What I want is:

  • if blue object is selected, red gameobject acts as an impassable wall to the blue gameobject.

  • if red object is selected, blue gameobject acts as an impassable wall to the red gameobject.

Here is the snippet of code I am using to make the selected gameobject move with keyboard input

 SelectedGameObject.rigidbody2D.velocity = new Vector2(xCom, yCom).normalized * 5;

Current settings for my two gameobjects are shown in the inspector

selected red character.png (159.5 kB)
nudged blue character.png (110.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Oliver1135 · Dec 26, 2014 at 10:52 PM

Slight update, In Unity it looks like if you have a collider without a rigidbody for all intents and purposes that object is considered static so the way I have hacked this is by adding and removing the rigidbody attached to all of the gameobjects based on whether I have it selected/am in control of moving it. Going to leave this question up for a day or two to see if anyone knows a 'better' way to do this else I'll just mark it as solved afterwards

 void Update ()
 {
     if (this.gameObject == GameManager.SelectedGameObject)
     {
         if (this.gameObject.rigidbody2D == null)
         {
             this.gameObject.AddComponent<Rigidbody2D>();
         }
     }
     else
     {
         if (this.rigidbody2D != null)
         {
             Destroy (this.rigidbody2D);
         }
     }
 }


Comment
Add comment · Show 2 · 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 Oliver1135 · Dec 29, 2014 at 06:14 PM 0
Share

I've been using this method for my little project for some time with no problems arisen or strange behavior co$$anonymous$$g out of it.

avatar image Kiwasi · Dec 29, 2014 at 06:19 PM 0
Share

The better method is to just set is$$anonymous$$inematic. Adding and removing physics components will probably hurt your performance significantly.

avatar image
1

Answer by AurimasBlazulionis · Dec 26, 2014 at 10:52 PM

I could suggest you use rigidbody constraints. But as it is 2D you can not because unity didin't implement it in 2d. So you can set the non selected objects rigidbody to kinematic by doing this:

Rigidbody2D.isKinematic = true

If that doesn't work then always set the not selected object's velocity to 0

Also don't forget to set fixedAngle value to true on the not selected object by doing this:

Rigidbody2D.fixedAngle = true

I hope it helps. It would be the way easier with constraints but I don't know why it's not implemented in 2D rigidbody

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

rigidbody2D.MovePosition() Can't Found 2 Answers

How to create an Asteroids style moving in 2D mode? 2 Answers

Improve ray detection? 0 Answers

increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer

Velocity doesnt change properly 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