Godot Version
Godot 4.5
Question
I am trying to move my camera in my RTS game.
- I’ve tried all sorts of different ways of doing this but they all fail; This works but its sloppy and I would prefer the camera to do nothing when pushing inputs for opposite directions.
This is what works
func _input(event: InputEvent):
if Input.is_action_pressed("MoveDown"):
global_position.y -= ScreenSpeed
elif Input.is_action_pressed("MoveUp"):
global_position.y += ScreenSpeed
elif Input.is_action_pressed("MoveRight"):
global_position.x -= ScreenSpeed
elif Input.is_action_pressed("MoveLeft"):
global_position.x += ScreenSpeed
else:
pass
Got a better way of doing this? preferably with a match statement?
- I’m out of Ideas and this style of movement upsets me. I need to move the camera with the mouse too and have no Idea how I am going to do THAT!!!
![]()
(I am avoiding a AI code or “Vide Coding”. {my main reason is public sentiment.})
UPDATE:
I’ve changed my way of doing this to the following (It doesn’t work but its closer to functioning the way I want then before so I will take it.) ![]()
# unstable variables
@export var Camera := Camera2D
@export var ScreenSpeed := 5
# Don't worry about this stuff it's leftover from a prevous attept and is useful for copy and paste.
#var InputActions: Array[String] = ["MoveDown", "MoveUp", "MoveRight", "MoveLeft"]
#var ScreenY = (Input.is_action_pressed("MoveDown") == true or Input.is_action_pressed("MoveUp") == false)
#var ScreenX = (Input.is_action_pressed("MoveRight") == true or Input.is_action_pressed("MoveLeft") == false)
func get_input():
var InputDirection = Input.get_vector("MoveDown", "MoveUp", "MoveRight", "MoveLeft")
velocity = InputDirection * ScreenSpeed
func _process(delta):
var MSD : get_input() * ScreenSpeed * delta #this does not work as intended yet.
global_position += MSD
FINAL PRODUCT
- This should not have taken so long and I am grateful for the patience from the wise Devs here who helped me.
- It has lead to this tiny little thing I should have been able to make to begin with but was too unlearned to make. Hopefully some other nooby dev can use this to shorten there development time by a lot.
var InputActions: Array[String] = ["MoveDown", "MoveUp", "MoveRight", "MoveLeft"] #THIS IS USEFUL GET OVER IT. This is a bunch of inputs in an array so I can copyandpastee
@export var ScreenSpeed := 500
func _process(delta: float):
# V This is a couple of inputs that make a Vector2, where the first input is positive and the second one is negative. V
var InputVector : Vector2 = Input.get_vector("MoveLeft", "MoveRight", "MoveUp", "MoveDown")
var velocity # this is currently empty
velocity = InputVector * ScreenSpeed * delta # Now we add some stuff.
position += velocity # Now we take our prevous litte math variable add it to the position.
print(velocity) # this is a test so we can see if it worked in the console.