ForumsProgramming ForumGuidance Code

10 5638
rytherix
offline
rytherix
69 posts
120

I have been looking (and failing) for a script that allows an object to guide to another object.
I am trying to create a guided missile, but I cant figure out how to make an mc guide to another mc at a constant speed and not
if (this._y>=target._y){
this._y-=2
}
because that will create an unnatural effect.
greatful for any help thanks =D

  • 10 Replies
dank
offline
dank
1,002 posts
960

You can put the first part in an on load event if you only want it to shoot towards it when fired. Else, you can just put it all in an on enter frame event.
Note that obj is the object it is tracking and YOUR_SPEED is the speed of the object that is tracking.

xDis =_root.obj._x-_x;
yDis =_root.obj._y-_y;
radAngle = Math.atan2(yDis, xDis);
_rotation = int((radAngle*180 / (Math.PI)));


if (_rotation > 180) {
_x -= (YOUR_SPEED * Math.sin( Math.PI/180 * _rotation));
_y += (YOUR_SPEED * Math.cos( Math.PI/180 * _rotation));
} else {
_x += (YOUR_SPEED * Math.sin( Math.PI/180 * _rotation));
_y -= (YOUR_SPEED * Math.cos (Math.PI/180 * _rotation));
}

PBMCube
offline
PBMCube
29 posts
80

Excellent code, Dank! If this flash actionscript is too much, then please refer to this site for a dialog on what you're attempting to do. It is a discussion on moving a movieclip and manipulation and helps explain some of what Dank is referring about.

Here are other sites that might help:
1) OOS - this guy is a genius!!!
2) Ed mack
3)TonyPa - who's very protective but has one of the best sites.

rytherix
offline
rytherix
69 posts
120

Dank, when I try this code out, the object moves away from the target object
heres what I am doin
onClipEvent (load) {
YOUR_SPEED=2
xDis = _root.obj._x-_x;
yDis = _root.obj._y-_y;
radAngle = Math.atan2(yDis, xDis);
_rotation = int((radAngle*180/(Math.PI)));
}
onClipEvent (enterFrame) {

if (_rotation>180) {
_x -= (YOUR_SPEED*Math.sin(Math.PI/180*_rotation));
_y += (YOUR_SPEED*Math.cos(Math.PI/180*_rotation));
} else {
_x += (YOUR_SPEED*Math.sin(Math.PI/180*_rotation));
_y -= (YOUR_SPEED*Math.cos(Math.PI/180*_rotation));
}
}
and if I put the beginning peice inside the Enterframe, it moves in a circular motion around the object lawl, what iam doing wrong?

dank
offline
dank
1,002 posts
960

Just swap out the _rotation in the on load with this.

_rotation = int((radAngle*180/(Math.PI))+180);

rytherix
offline
rytherix
69 posts
120

ok, idk what i am doing wrong here, so i uploaded a lil sample of what i am doing lol.
fp.dunadan.net/guidance.swf
if you click it reorients the rocket to a new position, but its not exactly moving towards the target if you know what i mean. heres the source code if you want to look into it more indepth
fp.dunadan.net/guidance.fla

dank
offline
dank
1,002 posts
960

try
_rotation = int((radAngle*180/(Math.PI))+90);

or
_rotation = int((radAngle*180/(Math.PI))-90);

firetail_madness
offline
firetail_madness
20,902 posts
2,710

I don't know what the heck is going here. Maybe you could search something on google????

rytherix
offline
rytherix
69 posts
120

woot +90 worked! thanks for all the help dank

not very helpful firetail, obviously I have tried those and failed, thats why i am on you know a FORUM

dank
offline
dank
1,002 posts
960

No problem, glad I could help

dank
offline
dank
1,002 posts
960

What's that supposed to mean?

Showing 1-10 of 10