Forums

ForumsProgramming Forum

Guidance Code

Thread Locked

Posted Jan 22, '08 at 2:36am

rytherix

rytherix

71 posts

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

 

Posted Jan 22, '08 at 9:16am

dank

dank

1,045 posts

Knight

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));
}

 

Posted Jan 24, '08 at 7:07pm

PBMCube

PBMCube

31 posts

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.

 

Posted Jan 25, '08 at 12:55am

rytherix

rytherix

71 posts

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?

 

Posted Jan 25, '08 at 9:12am

dank

dank

1,045 posts

Knight

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

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

 

Posted Feb 8, '08 at 8:41pm

rytherix

rytherix

71 posts

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

 

Posted Feb 9, '08 at 10:07am

dank

dank

1,045 posts

Knight

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

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

 

Posted Feb 9, '08 at 5:16pm

firetail_madness

firetail_madness

21,765 posts

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

 

Posted Feb 9, '08 at 7:59pm

rytherix

rytherix

71 posts

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

 

Posted Feb 9, '08 at 8:30pm

dank

dank

1,045 posts

Knight

No problem, glad I could help