- Home /
How to update EdgeCollider2D with sprite animation?
I have a sprite animation where I would like to have the collider being updated with every frame.
The animation tab does have a property field for EdgeCollider 2D but they include properties like density, trigger etc which aren't really helpful in changing the collider itself.
I also tried to change the collider while hitting the record button but the changes persist through every frame and the collider remains that way throughout the animation.
Any help in telling how to do this would be greatly appreciated.
Answer by UnitedCoders · Feb 01, 2018 at 01:57 PM
After two hours of struggle , finally I made this solution using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EdgeColliderUpdate : MonoBehaviour {
public int currentIndex;
public Vector2[] edgeColliderPointsIndex0;
public Vector2[] edgeColliderPointsIndex1;
Vector2[] _points;
public EdgeCollider2D _edgeCollider2D;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
_points = new Vector2[2];
}
public void IncrementIndex()
{
currentIndex++;
_points[0]=edgeColliderPointsIndex0 [currentIndex];
_points[1]=edgeColliderPointsIndex1 [currentIndex];
_edgeCollider2D.points = _points;
}
public void DecrementIndex()
{
currentIndex--;
_points[0]=edgeColliderPointsIndex0 [currentIndex];
_points[1]=edgeColliderPointsIndex1 [currentIndex];
_edgeCollider2D.points = _points;
}
}
In your Animation with every sprite call Increment or Decrement function using animation Event, and In the inspector assign your edge collider values with respect to your sprite.
Your answer
Follow this Question
Related Questions
How to make a sprite stay at a consistant baseline during animation? 1 Answer
2D sprite character movement 3 Answers
Disabling interpolation for Z rotation within animation window? 0 Answers
2d sprite animation how to set a button to let your sprite work 0 Answers
How to play an animation in reverse? 1 Answer