Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Dmitry-Volevodz · Mar 24, 2015 at 06:25 AM · 2d game

How to change Polygon Collider based on my animation?

I have a small car game, and when I move up/down and left/right, the sprite becomes different. But the physicsbody remains the same. How do I adjust physicsbody? I added a screenshot of my sprite. At the moment I have Polygon physics body as on the right one.

Here is the code that adjusts animation states:

 void FixedUpdate()
 {
     if (Input.GetKey(KeyCode.W)) {
         rb2d.AddForce(Vector2.up * physicsConstant);
         animator.CrossFade("CarUpIdle", 0);
     } else if (Input.GetKey(KeyCode.S)) {
         rb2d.AddForce(-Vector2.up * physicsConstant);
         animator.CrossFade("CarDownIdle", 0);
     } else if (Input.GetKey(KeyCode.D)) {
         rb2d.AddForce(Vector2.right * physicsConstant);
         animator.CrossFade("CarRightIdle", 0);
     } else if (Input.GetKey(KeyCode.A)) {
         rb2d.AddForce(-Vector2.right * physicsConstant);
         animator.CrossFade("CarLeftIdle", 0);
     }
 }



alt text

cars2.jpg (22.7 kB)
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 Cherno · Mar 24, 2015 at 11:13 AM 1
Share

How about creating the neccessary collisio meshes for the different sprites, then keeping them stored in variables and when the car changes direction as in your code, assigning the right mesh to the collider?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by basavaraj_guled · Jan 30, 2016 at 10:27 AM

 public bool iStrigger ;
         //public PhysicsMaterial2D _material ;
     
         private SpriteRenderer spriteRenderer;
         private List<Sprite> spritesList;
         private Dictionary<int, PolygonCollider2D> spriteColliders;
         private bool _processing ;
     
         private int _frame ;
         public int Frame {
                 get { return _frame; }
                 set {
                         if (value != _frame) {
                                 if (value > -1) {
                                         spriteColliders [_frame].enabled = false;
                                         _frame = value;
                                         spriteColliders [_frame].enabled = true;
                                 } else {
                                         _processing = true;
                                         StartCoroutine (AddSpriteCollider (spriteRenderer.sprite));
                                 }
                         }
                 }
         }
 
         private IEnumerator AddSpriteCollider (Sprite sprite)
         {
                 spritesList.Add (sprite);
                 int index = spritesList.IndexOf (sprite);
                 PolygonCollider2D spriteCollider = gameObject.AddComponent<PolygonCollider2D> ();
                 spriteCollider.isTrigger = iStrigger;
                 //    spriteCollider.sharedMaterial = _material;
                 spriteColliders.Add (index, spriteCollider);
                 yield return new WaitForEndOfFrame ();
                 Frame = index;
                 _processing = false;
         }
     
         private void OnEnable ()
         {
                 spriteColliders [Frame].enabled = true;
         }
     
         private void OnDisable ()
         {
                 spriteColliders [Frame].enabled = false;
         }
 
         private void Awake ()
         {
                 spriteRenderer = this.GetComponent<SpriteRenderer> ();
         
                 spritesList = new List<Sprite> ();
         
                 spriteColliders = new Dictionary<int, PolygonCollider2D> ();
         
                 Frame = spritesList.IndexOf (spriteRenderer.sprite);
         }
     
         private void LateUpdate ()
         {
                 if (!_processing)
                         Frame = spritesList.IndexOf (spriteRenderer.sprite);
         }
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 UnityCoach · Mar 17, 2016 at 01:01 PM 0
Share

Hi basavaraj_guled, I plan on using this code in a training course I want to publish. I mean to mention every contribution with proper credits. Please, let me know what you'd like me to put in the credits (name, email, url).

avatar image AdaptizTech · Apr 07, 2016 at 10:41 AM 0
Share

hi @jikkou, this is not my own written code, i found it unity answers, i just answered coz lot of people searching for this question

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i stop rigidbody2D bounce 1 Answer

player not moved after android build 0 Answers

Collision stops working at certain situations. Help? 1 Answer

How can i trigger death , set spawnpoints & add music? ( 2D ) 1 Answer

Multiple Start points 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