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:
whenclicked
setrotdir ▼to1foreverifkeyright arrow ▼pressed?setrotdir ▼to1changerotationspeed ▼by0.2elseifkeyleft arrow ▼pressed?setrotdir ▼to2changerotationspeed ▼by-0.2
whenclicked
forever ifnotrotationspeed=0ifrotdir=1changerotationspeed ▼by-0.1elsechangerotationspeed ▼by0.1turn
rotationspeeddegrees
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.
whenclicked
setFriction ▼to0.80 < Friction < 1, in general.setCurrentDirection ▼to0"Normal" direction, not Scratch direction.setVelocity.R ▼to0Rotational velocity.setVelocity.Max ▼to15Maximum velocity.setAcceleration.R ▼to0Rotational acceleration.setSpeed ▼to0.5Rotational speed.foreversetAcceleration.R ▼to0ifkeyright arrow ▼pressed?changeAcceleration.R ▼by-1*Speedifkeyleft arrow ▼pressed?changeAcceleration.R ▼by1*Speedifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.RchangeVelocity.R ▼byAcceleration.Rif>abs ▼ofVelocity.RVelocity.MaxsetVelocity.R ▼toVelocity.Max*/abs ▼ofVelocity.RVelocity.RchangeCurrentDirection ▼byVelocity.RsetCurrentDirection ▼toCurrentDirectionmod360point in direction90-CurrentDirection
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.
whenclicked
setFriction ▼to0.80 < Friction < 1, in general.setCurrentDirection ▼to0"Normal" direction, not Scratch direction.setVelocity.R ▼to0Rotational velocity.setVelocity.Max ▼to15Maximum velocity.setAcceleration.R ▼to0Rotational acceleration.setSpeed ▼to0.5Rotational speed.foreversetAcceleration.R ▼to0ifkeyright arrow ▼pressed?changeAcceleration.R ▼by-1*Speedifkeyleft arrow ▼pressed?changeAcceleration.R ▼by1*Speedifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.RchangeVelocity.R ▼byAcceleration.Rif>abs ▼ofVelocity.RVelocity.MaxsetVelocity.R ▼toVelocity.Max*/abs ▼ofVelocity.RVelocity.RchangeCurrentDirection ▼byVelocity.RsetCurrentDirection ▼toCurrentDirectionmod360point in direction90-CurrentDirection
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.
whenclicked
setFriction ▼to0.80 < Friction < 1, in general.setCurrentDirection ▼to0"Normal" direction, not Scratch direction.setVelocity.R ▼to0Rotational velocity.setVelocity.Max ▼to15Maximum velocity.setAcceleration.R ▼to0Rotational acceleration.setSpeed ▼to0.5Rotational speed.foreversetAcceleration.R ▼to0ifkeyright arrow ▼pressed?changeAcceleration.R ▼by-1*Speedifkeyleft arrow ▼pressed?changeAcceleration.R ▼by1*Speedifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.RchangeVelocity.R ▼byAcceleration.Rif>abs ▼ofVelocity.RVelocity.MaxsetVelocity.R ▼toVelocity.Max*/abs ▼ofVelocity.RVelocity.RchangeCurrentDirection ▼byVelocity.RsetCurrentDirection ▼toCurrentDirectionmod360point in direction90-CurrentDirectionAgghhhh, 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.
whenclicked
setFriction ▼to0.80 < Friction < 1, in general.setCurrentDirection ▼to0"Normal" direction, not Scratch direction.setVelocity.R ▼to0Rotational velocity.setVelocity.Max ▼to15Maximum velocity.setAcceleration.R ▼to0Rotational acceleration.setSpeed ▼to0.5Rotational speed.foreversetAcceleration.R ▼to0ifkeyright arrow ▼pressed?changeAcceleration.R ▼by-1*Speedifkeyleft arrow ▼pressed?changeAcceleration.R ▼by1*Speedifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.RchangeVelocity.R ▼byAcceleration.Rif>abs ▼ofVelocity.RVelocity.MaxsetVelocity.R ▼toVelocity.Max*/abs ▼ofVelocity.RVelocity.RchangeCurrentDirection ▼byVelocity.RsetCurrentDirection ▼toCurrentDirectionmod360point in direction90-CurrentDirectionAgghhhh, 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:
Because 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.ifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Acceleration.RCaught you, bug!
If you want, I can upload a version to my test account (although it looks like Wes64 got there first).ifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.R
...not...setVelocity.R ▼toFriction*Velocity.R
ifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.R
Last edited by amcerbu (2012-06-26 17:48:30)
Offline
amcerbu wrote:
ifnotkeyorright arrow ▼pressed?keyleft arrow ▼pressed?setVelocity.R ▼toFriction*Velocity.R
Here's a little habit of mine, I find it more convenient than the above code,
ifkeyright arrow ▼pressed?changevelocity ▼by-1elseifkeyleft arrow ▼pressed?changevelocity ▼by1elsesetvelocity ▼tofriction*velocity
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