Hello, I seem to be having an error with my code, this code is meant to make the object "look at the mouse" (changes rotation depending on mouse position), and when W is pressed, the object is supposed to move towards the position of the mouse. However, the object moves in a psuedo random direction, it changes drastically even with the slightest movements.
Also note that the rotational function works fine; the character always faces the mouse. However, the "move the direction you're facing" part doesn't work anymore, it DID work before I changed the rotational functions. Anyways, heres a snippet of the involved code:
onClipEvent (enterFrame) { ang = ((radians*180/Math.PI)); radians = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x); this._rotation = ang; facing = this._rotation *(180/Math.PI); if (Key.isDown(65)) { //blank for now } if (Key.isDown(87)) { this._x += Math.cos(facing)*moveSpeed; this._y += Math.sin(facing)*moveSpeed; // all code after this comment is irrelevant to movement/rotation
Any help would be greatly appreciated, it's probably a minor thinking or math error... thanks in advance
after like 2 hours I finally fixed it, I had to edit the 180 function a little, had to multiply the radians by Pi, then by 180 also... idk why but it worked :P
Alright well I know you fixed it, but the first thing I see is that you set "radians" after you set angle. Also, I'm pretty sure when you do Math.cos(facing) "facing" needs to be in radians, while you have it in degrees. I'm not positive on that though.
Well I understand degrees better than radians, and its easier to mathematically set an object's degrees than radians, especially since it glitched out. The rotation worked fine, I just had to switch this snippet of code:
facing = this._rotation*(180/Math.PI)
to
facing = this._rotation *Math.PI / 180
It's really weird how it came to me to fix that, I realized that he was rotating like exactly 180/3.14 times too fast... But its all fixed now