Sorry for what may be a simple question but I’ve been stuck on it.
I am attempting to script a character that walks until it hits a wall and then turns around and keeps walking. If it helps, this is similar behavior to lemmings.
What I have so far is a 2D rigid body to which I AddForce. It has a “feeler” object where I can detect the presence of “groud”. But that’s where I get lost. I’ve tried several things, the best I can do is flip the sprite but I can never get it to change directions.
How should this be done?
Is this in 2D left/right walking? If so you need a boolean to control if you’re facing left or right. I like to use a facingLeft boolean because then when the class starts, I’m facing right.
That boolean needs to directly control (also called “drive”) the following:
- the direction of your walking motion
- the flip X of your sprite
- the direction of your “feeler” stick ahead of you
That boolean does NOT necessarily need to flip the transform.rotation of your GameObject. In fact, doing that could lead to more problems than simply addressing the above items specifically, especially since you are using rigidbody physics.
Thank you very much for this answer. I was indeed trying to flip the whole object. Once I gave that up and followed the three points it all went much smoother. Lots of progress today thanks to you.
1 Like