Discuss Scratch

SemiTruk
Scratcher
17 posts

Smooth Sprite Turn

I'm making a top down game and I want the enemy to slowly and smoothly turn in the direction of the player, but I tried to do it and failed miserably. If anybody could help, that would be great.

“If at first you don't succeed, you fail.”
MineTurte
Scratcher
500+ posts

Smooth Sprite Turn

SemiTruk wrote:

I'm making a top down game and I want the enemy to slowly and smoothly turn in the direction of the player, but I tried to do it and failed miserably. If anybody could help, that would be great.

define findDirection
set [ originaldir] to (direction)
point towards [ player]
set [ direction2] to (direction)
point towards [ (originaldir)]
repeat until <(direction) = [(direction2)]>
turn cw (1) degrees
end

Hope this helps (I was kinda rushed so idk if it works lol)

(pronouns are she/her). Advanced Alpha Game Studios Manager. Our website: https://aags2.w3spaces.com
This is JuniperActias. AKA Moth mommy. If I helped you out consider following Juni-Bug (my new account)!




If you found this secret, say uwu
Signature gradients here













SemiTruk
Scratcher
17 posts

Smooth Sprite Turn

MineTurte wrote:

define findDirection
set [ originaldir] to (direction)
point towards [ player]
set [ direction2] to (direction)
point towards [ (originaldir)]
repeat until <(direction) = [(direction2)]>
turn cw (1) degrees
end

Hope this helps (I was kinda rushed so idk if it works lol)

Quick Question, does it run without screen refresh?

“If at first you don't succeed, you fail.”
RokCoder
Scratcher
1000+ posts

Smooth Sprite Turn

This is a better solution as it will always turn towards the player (as opposed to turning clockwise until it's in the right direction). Put a call to this custom block in the update script of your enemy so that it runs every frame

define Turn towards player
set [current direction v] to (direction)
point towards [player v]
set [target direction v] to (direction)
set [delta direction v] to (((((target direction) - (current direction)) + (180)) mod (360)) - (180))
if <([abs v] of (delta direction)) < (rotation speed)> then // rotation speed is the speed at which you want to turn towards the mouse pointer
point in direction (target direction)
else
point in direction ((current direction) + (((delta direction) / ([abs v] of (delta direction))) * (rotation speed)))
end


Scratch dabbling for fun…

MineTurte
Scratcher
500+ posts

Smooth Sprite Turn

RokCoder wrote:

This is a better solution as it will always turn towards the player (as opposed to turning clockwise until it's in the right direction). Put a call to this custom block in the update script of your enemy so that it runs every frame

define Turn towards player
set [current direction v] to (direction)
point towards [player v]
set [target direction v] to (direction)
set [delta direction v] to (((((target direction) - (current direction)) + (180)) mod (360)) - (180))
if <([abs v] of (delta direction)) < (rotation speed)> then // rotation speed is the speed at which you want to turn towards the mouse pointer
point in direction (target direction)
else
point in direction ((current direction) + (((delta direction) / ([abs v] of (delta direction))) * (rotation speed)))
end
Yeah I knew that turning always right would be a problem. I would've made it correct if I had more time lol.

(pronouns are she/her). Advanced Alpha Game Studios Manager. Our website: https://aags2.w3spaces.com
This is JuniperActias. AKA Moth mommy. If I helped you out consider following Juni-Bug (my new account)!




If you found this secret, say uwu
Signature gradients here













SemiTruk
Scratcher
17 posts

Smooth Sprite Turn

RokCoder wrote:

This is a better solution as it will always turn towards the player (as opposed to turning clockwise until it's in the right direction). Put a call to this custom block in the update script of your enemy so that it runs every frame

define Turn towards player
set [current direction v] to (direction)
point towards [player v]
set [target direction v] to (direction)
set [delta direction v] to (((((target direction) - (current direction)) + (180)) mod (360)) - (180))
if <([abs v] of (delta direction)) < (rotation speed)> then // rotation speed is the speed at which you want to turn towards the mouse pointer
point in direction (target direction)
else
point in direction ((current direction) + (((delta direction) / ([abs v] of (delta direction))) * (rotation speed)))
end

Thks for the help! This worked great.


“If at first you don't succeed, you fail.”
the98phases
Scratcher
8 posts

Smooth Sprite Turn

@RokCoder So after i do:
when green flag clicked
forever
wait (1) secs
create clone of [ myself]
end
when I start as a clone
point towards [ Player]
move (-200) steps
forever
endif <[ID] = [1]> then
switch costume to [ Square]
(Turn Block)
end
?
The turn block is in the If/Then script

Last edited by the98phases (March 24, 2024 17:38:02)

the98phases
Scratcher
8 posts

Smooth Sprite Turn

RokCoder wrote:

This is a better solution as it will always turn towards the player (as opposed to turning clockwise until it's in the right direction). Put a call to this custom block in the update script of your enemy so that it runs every frame

define Turn towards player
set [current direction v] to (direction)
point towards [player v]
set [target direction v] to (direction)
set [delta direction v] to (((((target direction) - (current direction)) + (180)) mod (360)) - (180))
if <([abs v] of (delta direction)) < (rotation speed)> then // rotation speed is the speed at which you want to turn towards the mouse pointer
point in direction (target direction)
else
point in direction ((current direction) + (((delta direction) / ([abs v] of (delta direction))) * (rotation speed)))
end

So i have a few questions. 1, Are these all open variables?
2, what should i do with the block? (example down here)
when green flag clicked
forever
point towards [ player]
move (var) steps
Then the custom block
end
RokCoder
Scratcher
1000+ posts

Smooth Sprite Turn

You call the Turn towards player custom block to carry out the rotation towards the player - it will need to be in an update loop or similar. That said, I pointed to the response in this post because it contains the code you need to use. It's not regarded as good forum etiquette to piggyback on someone else's post in this manner though. You should start your own thread.


Scratch dabbling for fun…

Powered by DjangoBB