# Thread: random movement AND make the mc face in the direction that it is moving

1. 68
posts
Registered User

## random movement AND make the mc face in the direction that it is moving

I figured out the random movement part. Well, I didn't figure it out, I looked around. I found one that is way simpler than the tutorial on Kirupa. Btw, this is a game where the player is looking straight down (from a bird's eye type thing). I would like my movieclip to rotate to face the direction that it is moving. Any suggestions?

Here's the random movement code that I found if anyone is interested...

Code:
```onClipEvent(load){
xDir=random(3)-1;
yDir=random(3)-1;
}
onClipEvent(enterFrame){
if (random(10) == 0){
xDir=random(3)-1;
}
if (random(10) == 0){
yDir=random(3)-1;
}
_x+=xDir;
_y+=yDir;
}```
Thanks for the help!

2. 42
posts
Registered User

Code:
```if (random(10) == 0){
angle = random(360);
}
if (random(10) == 0){
speed = random(2);
}
_x += Math.cos(angle * Math.PI / 180) * speed;
_y += Math.sin(angle * Math.PI / 180) * speed;
// x += cos(angle), y += sin(angle) is trig to turn an angle into coordinates
_rotation = angle;```

3. 10
posts
Registered User
To face the direction you are moving do:

dx and dy are the distance moved since the last frame. So keep variables of the old location (ox, oy)

dx = x - ox;
dy = y - oy
var r = Math.atan2(dy, dx);
var d = r * 180 / Math.PI-90
if (d > 180) d -= 360;
if (d < -180) d += 360;

obj.rotation = d;

ox = x;
oy = y

if it is too jittery add some smoothing to the movement

4. 68
posts
Registered User

## Thanks!

Originally Posted by tapioca

Code:
```if (random(10) == 0){
angle = random(360);
}
if (random(10) == 0){
speed = random(2);
}
_x += Math.cos(angle * Math.PI / 180) * speed;
_y += Math.sin(angle * Math.PI / 180) * speed;
// x += cos(angle), y += sin(angle) is trig to turn an angle into coordinates
_rotation = angle;```
I was thinking of trying to do something like this, but my trig skills just weren't up to the challenge. Thank you very much!