0 Favourites

# Pathfinding and Rotating

• 13 posts
• Hey guys, love Construct 2, hats of to the creators for doing an absolutely amazing job, making it easier for us to make games.

I need slight help with pathfinding. Here's what I'm trying to do.

When following a path, when moving left the object should rotate left 20 degrees and and moving right the object should rotate right 20 degrees.

It should not rotate fully and should face upwards when moving up or moving down.

Does that make sense?

Would love some help on this.

Thanks,

Dan

• Is the object your player, controlled by the keyboard or mouse? Or is it a non-player Sprite controlled by your game "AI"?

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Hi Matt, it's a non player sprite, basically a sprite following a path.

Thanks,

Dan

• I think you can do this with an action e.g. rotate 20 degrees clockwise (or rotate towards angle x), but if the sprite then has an action move forward it would move towards its angle, in which case you could use move at angle.

Caveat: It's based on my understanding of C2, I've not tried this.

• One way would be to store the last x position in an instance variable, and have the target angle in another instance variable. Check every tick if the current x is greater or less than the last x, and set the target angle accordingly. Have another event that rotates the sprite towards the target angle every tick for smooth rotation.

• Thanks for the response guys, Oosyrag, I tried your method, how would I store last x position in the instance variable "LastX"?

I've tried Enemy X=0      set LastX to Enemy.X

It rotates right but then it doesn't rotate back or left when moving left.

I setup the rotation as follows:

Every tick                     Rotate 1 degrees towards 20

Enemy LastX < Enemy.X

Every tick                     Rotate 1 degrees towards -20

Enemy - LastX > Enemy.X

Thanks,

Dan

• It's ok I figured it out. I tried "Every tick set lastx to Enemy.X" but that wasn't allowing the object to rotate so instead I set "Every tick" to "Every 0.5 secs" and the object is rotating as it should. Now the challenge is to get it to rotate back to default position when not moving left or right.

Dan

• sprite.x>sprite.lastx       set sprite.targetangle to +20

trigger once

sprite.x<sprite.lastx       set sprite.targetangle to -20

trigger once

every tick                  rotate 1 degree towards targetangle

set sprite.lastx = sprite.x

Make sure you have these events occur after the movement of the sprite. The trick is to compare current x and last x before last x is updated to current x.

You will also need to sub these events for your two situations, one set for upwards facing and one set for downwards facing. You can also add if X didn't change, set the angle to 0 or 180 to have it face up or down.

(actually you should use rotate 60*dt degrees instead of 1 degree, see scirra.com/tutorials/67/delta-time-and-framerate-independence)

• Thanks for that, I did figure out how to rotate the object finally but now need to figure out how to rotate back to default position (facing up) when not going left or right.

How would you add "if X didn't change"?

Thanks,

Dan

• sprite.x=sprite.lastx

• Yeah I did...

Sprite.X=Sprite.lastx      Sprite Rotate 1 degrees towards 0

But no success.

Dan

• Just got back to a workstation with Construct. Here is a capx:

• That's so generous of you. Thank you very much. I tried that structure on my project but it still doesn't straighten when not going left or rights. Maybe it's got something to do with the followpath event.

I kind of got it to straighten by using the events:

Sprite TargetAngle = 20             Sprite Set TargetAngle to 0

Sprite LastY < Sprite.Y

Sprite TargetAngle = -20             Sprite Set TargetAngle to 0

Sprite LastY > Sprite.Y

It still doesn't work properly though, it doesn't straighten in some places and doesn't rotate it some places.

Thanks again for your help though, I appreciate it.

Dan

• 13 posts