# Random Motion

**URL:** <https://forum.kirupa.com/t/random-motion/47749>\
**Category:** Uncategorized\
**Created:** [April 6, 2004, 10:43pm UTC](https://forum.kirupa.com/t/random-motion/47749 "2004-04-06T22:43:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yeldarb](https://avatars.discourse-cdn.com/v4/letter/y/51bf81/32.png) [@Yeldarb](https://forum.kirupa.com/u/Yeldarb)\
**Post date:** [April 6, 2004, 10:43pm UTC](https://forum.kirupa.com/t/random-motion/47749/1 "2004-04-06T22:43:52Z")

</div>

I am trying to make the movieClip named “fish” go to a randomly generated spot. I tried using the tutorial, but I didn’t understand the math, so I tried it with my own code. I’m not as much interested in getting this to work as I am knowing why it doesn’t work. So if someone could please explain why my code doesn’t work, I’d be very appreciative.

```php

fish.onLoad = function() {
	generatePoint();
}
fish.onEnterFrame = function() {
	x = _x;
	y = _y;
	changeX = pointX-x;
	changeY = pointY-y;
	slope = changeY/changeX;
	goX = speed;
	goY = slope*speed;
	if(_x<pointX) {
		_x += goX;
	} else {
		_x -= goX;
	}
	if(_y<pointY) {
		_y += goY;
	} else {
		_y -= goY;
	}
	if(hitTest(pointX, pointY, true)) {
		generatePoint();
	}
}
function generatePoint() {
	pointX = random(Stage.width);
	pointY = random(Stage.height);
	speed = random(10)+1;
}

```

Thanks :thumb:
