# Pathfinding and Rotating

Get help using Construct 2

### » Mon Mar 04, 2013 4:21 pm

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
B
8
S
1
Posts: 65
Reputation: 1,103

### » Mon Mar 04, 2013 5:12 pm

Is the object your player, controlled by the keyboard or mouse? Or is it a non-player Sprite controlled by your game "AI"?
B
6
S
2
G
3
Posts: 10
Reputation: 2,402

### » Mon Mar 04, 2013 5:13 pm

Hi Matt, it's a non player sprite, basically a sprite following a path.

Thanks,
Dandankhan2013-03-04 17:15:01
B
8
S
1
Posts: 65
Reputation: 1,103

### » Tue Mar 05, 2013 4:49 am

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.
B
7
Posts: 38
Reputation: 476

### » Tue Mar 05, 2013 7:01 am

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.
B
36
S
16
G
67
Posts: 1,038
Reputation: 38,080

### » Tue Mar 05, 2013 4:44 pm

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
B
8
S
1
Posts: 65
Reputation: 1,103

### » Tue Mar 05, 2013 8:23 pm

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.

Dandankhan2013-03-05 20:24:44
B
8
S
1
Posts: 65
Reputation: 1,103

### » Tue Mar 05, 2013 8:36 pm

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 https://www.scirra.com/tutorials/67/delta-time-and-framerate-independence)
B
36
S
16
G
67
Posts: 1,038
Reputation: 38,080

### » Tue Mar 05, 2013 9:30 pm

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
B
8
S
1
Posts: 65
Reputation: 1,103

### » Tue Mar 05, 2013 9:46 pm

sprite.x=sprite.lastx
B
36
S
16
G
67
Posts: 1,038
Reputation: 38,080

Next