Hey guys, quick post. I am desperatly looking for a script that will correctly create a rotating object that is controlled by the left and righ arrow keys, I need it to gain speed, and if you stop pressing it, loose speed gradually. Here is a link to my project I need it for. http://scratch.mit.edu/projects/daniel_j/2634226You will notice my script for that failed... If you can fix it please let me know. This project is really important to me and I need it by friday, that i when it is due!! Sorry this post is rushed
Thanks Dan Jones.!
Offline
You need 2 variables:
rotationspeed
rotdir
and then, these scripts:
when gf clicked set [rotdir v] to [1] forever if <key [right arrow v] pressed?> set [rotdir v] to [1] change [rotationspeed v] by (0.2) else if <key [left arrow v] pressed?> set [rotdir v] to [2] change [rotationspeed v] by (-0.2) end end end
when gf clicked forever if <not <(rotationspeed) = [0]>> if <(rotdir) = [1]> change [rotationspeed v] by (-0.1) else change [rotationspeed v] by (0.1) end turn cw (rotationspeed) degrees end
Last edited by SciTecCf (2012-06-25 09:24:45)
Offline
I would recommend this script. Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc. This allows you to perform trigonometric functions accurately. Adjust the starting values for Friction, Velocity.Max, and Speed, as you like. They control how fast the object accelerates, how quickly it slows down, and how fast it can go. Make sure that friction is a positive number less than 1 (if you want it to slow down). Numbers closer to 0 will cause it to slow down more quickly.
when gf clicked set [Friction v] to [0.8] // 0 < Friction < 1, in general. set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction. set [Velocity.R v] to [0] // Rotational velocity. set [Velocity.Max v] to [15] // Maximum velocity. set [Acceleration.R v] to [0] // Rotational acceleration. set [Speed v] to [0.5] // Rotational speed. forever set [Acceleration.R v] to [0] if <key [right arrow v] pressed?> change [Acceleration.R v] by ((-1) * (Speed)) end if <key [left arrow v] pressed?> change [Acceleration.R v] by ((1) * (Speed)) end if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) end change [Velocity.R v] by (Acceleration.R) if < ([abs v] of (Velocity.R)) > (Velocity.Max) > set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R))) end change [CurrentDirection v] by (Velocity.R) set [CurrentDirection v] to ((CurrentDirection) mod (360)) point in direction ((90) - (CurrentDirection)) end
Offline
sanjayraj wrote:
Is this what you need?
when gf clicked forever turn (15) degreesThat's a rotation script...
Hhehe sanjayraj, thanks, but I am looking for a script like the one above ^^ xD thanks anyway!
Offline
amcerbu wrote:
I would recommend this script. Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc. This allows you to perform trigonometric functions accurately. Adjust the starting values for Friction, Velocity.Max, and Speed, as you like. They control how fast the object accelerates, how quickly it slows down, and how fast it can go. Make sure that friction is a positive number less than 1 (if you want it to slow down). Numbers closer to 0 will cause it to slow down more quickly.
when gf clicked set [Friction v] to [0.8] // 0 < Friction < 1, in general. set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction. set [Velocity.R v] to [0] // Rotational velocity. set [Velocity.Max v] to [15] // Maximum velocity. set [Acceleration.R v] to [0] // Rotational acceleration. set [Speed v] to [0.5] // Rotational speed. forever set [Acceleration.R v] to [0] if <key [right arrow v] pressed?> change [Acceleration.R v] by ((-1) * (Speed)) end if <key [left arrow v] pressed?> change [Acceleration.R v] by ((1) * (Speed)) end if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) end change [Velocity.R v] by (Acceleration.R) if < ([abs v] of (Velocity.R)) > (Velocity.Max) > set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R))) end change [CurrentDirection v] by (Velocity.R) set [CurrentDirection v] to ((CurrentDirection) mod (360)) point in direction ((90) - (CurrentDirection)) end
Agghhhh, I followed your script exactly, and it is awesome, but it does not slow down, it just instantly stops...? I followed your instructions as best as I could..... any ideas?
Thanks
Dan Jones
Offline
daniel_j wrote:
amcerbu wrote:
I would recommend this script. Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc. This allows you to perform trigonometric functions accurately. Adjust the starting values for Friction, Velocity.Max, and Speed, as you like. They control how fast the object accelerates, how quickly it slows down, and how fast it can go. Make sure that friction is a positive number less than 1 (if you want it to slow down). Numbers closer to 0 will cause it to slow down more quickly.
when gf clicked set [Friction v] to [0.8] // 0 < Friction < 1, in general. set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction. set [Velocity.R v] to [0] // Rotational velocity. set [Velocity.Max v] to [15] // Maximum velocity. set [Acceleration.R v] to [0] // Rotational acceleration. set [Speed v] to [0.5] // Rotational speed. forever set [Acceleration.R v] to [0] if <key [right arrow v] pressed?> change [Acceleration.R v] by ((-1) * (Speed)) end if <key [left arrow v] pressed?> change [Acceleration.R v] by ((1) * (Speed)) end if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) end change [Velocity.R v] by (Acceleration.R) if < ([abs v] of (Velocity.R)) > (Velocity.Max) > set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R))) end change [CurrentDirection v] by (Velocity.R) set [CurrentDirection v] to ((CurrentDirection) mod (360)) point in direction ((90) - (CurrentDirection)) endAgghhhh, I followed your script exactly, and it is awesome, but it does not slow down, it just instantly stops...? I followed your instructions as best as I could..... any ideas?
Thanks
Dan Jones
Did you try mine?
Offline
omg, amcerbu, I coud not for the love of me get this script working in my game...
If anyone could just download the script for me and add it, I would be so * appreciative! Sorry for all your troubles....
Dan Jones
Thanks
Offline
Here you go.
I simplified it a ton, amercbu, your script is overly complicated. Remember, not every user is as math-oriented as you. ;D
Anyways, dan, here is a very basic rotation script. You can change the (.94) in the script. A smaller fraction will make it slower, a larger fraction will make it very fast.
Offline
Wes64 wrote:
Here you go.
I simplified it a ton, amercbu, your script is overly complicated. Remember, not every user is as math-oriented as you. ;D
Anyways, dan, here is a very basic rotation script. You can change the (.94) in the script. A smaller fraction will make it slower, a larger fraction will make it very fast.
Thanks wes64, you amcerbu and scifitech have been huge helps
Offline
SciTecCf wrote:
daniel_j wrote:
amcerbu wrote:
I would recommend this script. Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc. This allows you to perform trigonometric functions accurately. Adjust the starting values for Friction, Velocity.Max, and Speed, as you like. They control how fast the object accelerates, how quickly it slows down, and how fast it can go. Make sure that friction is a positive number less than 1 (if you want it to slow down). Numbers closer to 0 will cause it to slow down more quickly.
when gf clicked set [Friction v] to [0.8] // 0 < Friction < 1, in general. set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction. set [Velocity.R v] to [0] // Rotational velocity. set [Velocity.Max v] to [15] // Maximum velocity. set [Acceleration.R v] to [0] // Rotational acceleration. set [Speed v] to [0.5] // Rotational speed. forever set [Acceleration.R v] to [0] if <key [right arrow v] pressed?> change [Acceleration.R v] by ((-1) * (Speed)) end if <key [left arrow v] pressed?> change [Acceleration.R v] by ((1) * (Speed)) end if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) end change [Velocity.R v] by (Acceleration.R) if < ([abs v] of (Velocity.R)) > (Velocity.Max) > set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R))) end change [CurrentDirection v] by (Velocity.R) set [CurrentDirection v] to ((CurrentDirection) mod (360)) point in direction ((90) - (CurrentDirection)) endAgghhhh, I followed your script exactly, and it is awesome, but it does not slow down, it just instantly stops...? I followed your instructions as best as I could..... any ideas?
Thanks
Dan JonesDid you try mine?
I did very good thankyou!
Offline
You ALL are going overboard.
(Look below for proof)
var rot = 0
if right arrow key down
var rot +1
if (var rot)> 0
var rot -0.25
if right arrow key down
var rot -1
if (var rot)< 0
var rot +0.25
rotate (var rot) degrees
This is the basic velocity script I always use.
Hope I helped.
Last edited by pipercubjl (2012-06-26 16:40:43)
Offline
Wes64 wrote:
Here you go.
I simplified it a ton, amercbu, your script is overly complicated. Remember, not every user is as math-oriented as you. ;D
Yeah, sorry about that daniel_j. Math is my favorite subject, and I tend to get carried away a bit...
About the script not working:
I think you made a small mistake copying it into Scratch. That's why it would stop immediately after the keys were released.
Your program says:
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Acceleration.R)) // Caught you, bug! endBecause of previous statements that set Acceleration.R to 0 (and ensure it remains at 0 if no keys are pressed), the object will stop immediately when you let go of the arrows, as Velocity.R will equal 0.
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) endIf you want, I can upload a version to my test account (although it looks like Wes64 got there first).
set [Velocity.R v] to ((Friction) * (Velocity.R))...not...
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) end
Last edited by amcerbu (2012-06-26 17:48:30)
Offline
amcerbu wrote:
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>> set [Velocity.R v] to ((Friction) * (Velocity.R)) end
Here's a little habit of mine, I find it more convenient than the above code,
if <key [right arrow v] pressed?> change [velocity v] by [-1] else if <key [left arrow v] pressed?> change [velocity v] by [1] else set [velocity v] to <(friction) * (velocity)> end end
Offline
^^ The only problem with that script is that if both keys are pressed, the program will see a right keypress and continue accelerating right, rather than slowing down. It turns out to work more smoothly if friction is applied constantly, without waiting until the arrow keys aren't pressed.
Offline
pipercubjl wrote:
You ALL are going overboard.
(Look below for proof)
var rot = 0
if right arrow key down
var rot +1
if (var rot)> 0
var rot -0.25
if right arrow key down
var rot -1
if (var rot)< 0
var rot +0.25
rotate (var rot) degrees
This is the basic velocity script I always use.
Thanks
Hope I helped.
Offline