• 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 coming 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

rigidbody2D.MovePosition() Can't Found 2 Answers

Limit mouse position so that player launches same distance 2 Answers

Different collision results in some areas. 1 Answer

how to calculate force direction on mouse up 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges