Discuss Scratch

Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

Hi,

I am making a car game and I am wondering how to code camera movement so that the camera view can follow the car.
Thank you!
bcs320jn
Scratcher
100+ posts

Need Help for coding Camera Movement

What type is it? Top-down, etc.?

Last edited by bcs320jn (March 15, 2022 20:51:37)

Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

bcs320jn wrote:

What type is it? Top-down, etc.?
I'm sorry, what is top-down?
Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

It's a car race game and I want the camera to follow the car. It has loops and turns, not like a straight line.
bcs320jn
Scratcher
100+ posts

Need Help for coding Camera Movement

Could you please provide a link to the project? That would make it a lot easier to understand.
Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

The problem is that all I have in the project is the car design and the map. Do you just want the picture of the map?
bcs320jn
Scratcher
100+ posts

Need Help for coding Camera Movement

No thanks, I do not need a picture of the map. I would suggest this for the code:

1: basic movement and camera following

*In the ‘map’ sprite, add a new (empty) costume. Name this ‘Blank’.
*Next, go to the ‘car’ sprite. Add this code in:
whenclickedpointindirection0foreverturnkeyright arrowpressed?-keyleft arrowpressed?*3degrees
This just allows you to control the direction of the car with the arrow keys.
*Then, go back to the map sprite. Put in this code:
whenclickedsetrotationstyleDon't rotateswitchcostumetoBlanksetsizetoAnywhere between 100 and 1000, depending on how big the 'map' costume is%switchcostumetoMapforeverpointindirectionDirectionofCarmovekeydown arrowpressed?-keyup arrowpressed?*3steps
This allows you to move forwards and backwards on the map.

2: Adding boundaries

For this, you need to find the width and height of the ‘map’ costume. If you don't know how to get this, I will respond later with an answer.
For adding boundaries, put this code under the previous code:
ifxposition>Widthof'map'costume/2*size/100thensetxtoWidthof'map'costume/2*size/100elseifxposition<-1*Widthof'map'costume/2*size/100thensetxto-1*Widthof'map'costume/2*size/100ifyposition>Heightof'map'costume/2*size/100thensetytoHeightof'map'costume/2*size/100elseifyposition<-1*Heightof'map'costume/2*size/100thensetyto-1*Heightof'map'costume/2*size/100
If this doesn't work in any way, please tell me.
Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

bcs320jn wrote:

snip
Can you explain to me what adding boundaries is? And how to find the height and width of the map costume?

Last edited by Enigma_Ezra (April 27, 2022 16:46:37)

bcs320jn
Scratcher
100+ posts

Need Help for coding Camera Movement

Boundaries being there so that the map goes off-screen if you go too far out.
bcs320jn_test
Scratcher
13 posts

Need Help for coding Camera Movement

For the width and height, this project should help:
https://scratch.mit.edu/projects/659775075/
Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

Okay, thank you! Also, my project doesn't use arrow keys, they use the direction of the mouse (including when backing up ). Also, the car can go when the mouse is down (so like the car can't move when the mouse is up). How do I change the code?

Last edited by Enigma_Ezra (March 15, 2022 23:55:39)

bcs320jn_test
Scratcher
13 posts

Need Help for coding Camera Movement

First, replace the car code with this:
whenclickedforeverpointtowardsmouse pointer
And in the map sprite, replace the
movekeyright arrowpressed?-keyleft arrowpressed?*-3steps
With
movemousedown?*-3steps

Last edited by bcs320jn_test (March 16, 2022 01:25:17)

bcs320jn_test
Scratcher
13 posts

Need Help for coding Camera Movement

I made a little oopsie in the barriers code. I’ll fix it in the morning and tell you. (Sorry!)
Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

bcs320jn_test wrote:

I made a little oopsie in the barriers code. I’ll fix it in the morning and tell you. (Sorry!)

Okay, I just saw these in the morning anyway so I can wait.
Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

bcs320jn wrote:

snip
Got it! Thank you so much!

Last edited by Enigma_Ezra (April 27, 2022 16:47:26)

Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

bcs320jn wrote:

snip
I can't put the boundaries code under a forever loop though…

Last edited by Enigma_Ezra (April 27, 2022 16:47:45)

bcs320jn_test
Scratcher
13 posts

Need Help for coding Camera Movement

Sorry, put that under the code IN the forever loop

Last edited by bcs320jn_test (March 16, 2022 17:21:46)

Enigma_Ezra
Scratcher
500+ posts

Need Help for coding Camera Movement

bcs320jn_test wrote:

Sorry, put that under the code IN the forever loop
Okay. Did you change the boundaries code yet?

Last edited by Enigma_Ezra (March 16, 2022 17:25:26)

Picoguy
Scratcher
100+ posts

Need Help for coding Camera Movement

bcs320jn wrote:

snip

What does
keyleft arrowpressed?-keyright arrowpressed?*3
do?
bcs320jn_test
Scratcher
13 posts

Need Help for coding Camera Movement

ifxposition>Widthof'map'costume/2*size/100thensetxtoWidthof'map'costume/2*size/100elseifxposition<-1*Widthof'map'costume/2*size/100thensetxto-1*Widthof'map'costume/2*size/100ifyposition>Heightof'map'costume/2*size/100thensetytoHeightof'map'costume/2*size/100elseifyposition<-1*Heightof'map'costume/2*size/100thensetyto-1*Heightof'map'costume/2*size/100
The code above was wrong. Here is the fixed version:
ifxposition>Widthof'map'costume/2*size/100+240thensetxtoWidthof'map'costume/2*size/100+240elseifxposition<Widthof'map'costume/2*size/100+240*-1thensetxtoWidthof'map'costume/2*size/100+240*-1ifyposition>Heightof'map'costume/2*size/100+180thensetytoHeightof'map'costume/2*size/100+180elseifyposition<Heightof'map'costume/2*size/100+180*-1thensetytoHeightof'map'costume/2*size/100+180*-1
You'll have to look closely, because it was a small change. (Adding 240 or 180 to the boundaries limits)

Last edited by bcs320jn_test (March 16, 2022 17:33:38)

Powered by DjangoBB