- Home /
The question is answered, right answer was accepted
how do i change the size of the collider when changing animation
you see, i have a character in 2d that is sorta like a spider, but with 4 legs, when he walks, the legs spread a little wider than when he stays still, the thing is, i need to change the collider when he is walking
In fact, i also noticed that the walking sprite has an offset because is a different size than the idle sprite, how do i change the offset of one sprite without affecting the others, and how do i change the collider only when is in the walking animation?
Answer by MSavioti · Feb 27, 2021 at 08:05 PM
how do i change the offset of one sprite without affecting the others
select the sprite you want to modify, open the sprite editor and will see that each frame has a blue circle. It's pivot, a reference of where is the center of the sprite which you can adjust.
how do i change the collider only when is in the walking animation?
Have a reference to the collider in your code and change the collider.size
from there. To know when to do this, set up Animation Events in the walk animation.
PS.: Avoid making more than one question per post.
thanks, but how does collider.size works, i dont know much about coding yet...
for example if my original collider is its a rectangle of 19 pixels in x and 12 pixels in y and I want to change it to 23 pixels in x but keep the y, how do i put that in code¿
Create a variable Collider2D in your code and assign it (inspector or
GetComponent()
).Let's say it's called
_collider
. In your code you do_collider.size = new Vector2(x, y)
How do you know the values for x and y? They're the same value you set in the inspector, so you can test the values from there and adjust accordingly.