Wednesday, June 29, 2022
HomeGame Developmentunity - How you can flip enemy patrolling facet to facet with...

unity – How you can flip enemy patrolling facet to facet with Mathf.PingPong?


The issue with the Mathf.PingPong operate is that it does not inform you if the returned worth is from the “Ping” or the “Pong” section. If you want that info, for instance since you want it to determine wherein path the sprite is meant to face, then it’s not going to be of a lot use. So we’re going to want to make use of a distinct resolution right here.

One easy resolution is to have a variable which states if the bunny is presently shifting left or proper. I would favor to make use of an enum for that objective, however when you do not know find out how to use enumerations but, then you’ll be able to simply use a flag bool isMovingLeft.

Then you definitely implement the Replace operate to behave in another way relying on the state of that variable:

if (isMovingLeft) {
    remodel.Translate(-movementSpeed * Time.deltaTime, 0f, 0f);
} else {
    remodel.Translate(movementSpeed * Time.deltaTime, 0f, 0f);
}

Then it’s essential to detect if an occasion occurred which might trigger the bunny to show round, after which do every little thing that entails. In that case this occasion can be to exceed the “max” or “min” worth:

if (remodel.place.x > max) {
    isMovingLeft = true; // transfer left any more       
    remodel.scale.x = -1f;  // flip the sprite to mirrored
}
if (remodel.place.x < min) {
    isMovingLeft = false; // transfer proper once more any more
    remodel.scale.x = 1f; // flip the sprite again to regular
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments