0 votes

How would I create a parallax background which moves according to the position of the mouse? If you could, please show some example script

Godot version 3.2
in Engine by (297 points)

1 Answer

+1 vote

Well, if you have a camera the parallax background will follow the camera movement, in that case i think the simpler option is to create your own moving background.

If you do not have a camera, you can set up the parallax background and layers in the tree

O: SceneRoot
- []: ParallaxBackground
- - []: ParallaxLayer1
- - - O: Sprite1
- - []: ParallaxLayer2 #set different scale if you want them to move at differen speed
- - - O: Sprite2

Then you can control everything with

$ParallaxBackground.scroll_offset=get_global_mouse_position()
by (1,436 points)
get_global_mouse_position() isn't declared in the current class

Do I have to create a variable for it?

no, you are probably just calling get_global_mouse_position() from a node that does not support this method, likely something that does not inherits from a canvas item.
what node is this script attached to?

ParallaxBackground

yep, that's it, parallaxBackground extend from CanvasLayer, and do not support the get mouse position methods (in short, it is not a node related to the viewport directly, it does not know the position of the mouse. it is like asking the AudioStreamer node to tell you the mouse position)

you should call it from a canvasItem node, like Node2D or Control.
EG:

O: Node2D <==PLACE SCRIPT HERE ($ParallaxBackground.scroll_offset=get_global_mouse_position())
- []: ParallaxBackground<== or place it here ( scroll=get_parent ().get_global_mouse_position() l
- - []: ParallaxLayer1
- - - O: Sprite1
- - []: ParallaxLayer2 #set different scale if you want them to move at differen speed
- - - O: Sprite2

Do I need to add anything because it parallax doesn't move? No errors this time though

difficult to answer, paste the code to clarify

extends Node2D

func _ready():
    ($ParallaxBackground.scroll_offset=get_global_mouse_position())
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.