- Home /
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);
}
}
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?
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);
}
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).
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
![](https://koobas.hobune.stream/wayback/20220613191050im_/https://answers.unity.com/themes/thub/images/avi.jpg)