Is there a better way to move my Camera2D or viewport?

Godot Version

Godot 4.5

Question

I am trying to move my camera in my RTS game.

:face_with_bags_under_eyes: - 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?
:melting_face: - 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!!! :rofl: :rofl: :rofl: :rofl:

(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.) :grin:

# 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

:face_holding_back_tears: - This should not have taken so long and I am grateful for the patience from the wise Devs here who helped me.

:grin: - 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.

Use input.get_vector(). Multiply the vector by speed and delta time and add it to global_position

Just a note. You can just delete this as it does nothing. An if does not have to have an else.

1 Like

:melting_face: - Thanks for the help this really cleared some stuff up. I Still haven’t completely fixed my problem but this is a start, after reading your reply I didn’t completely understand what you meant so I read up on get_vector() This is a good example:[ 2D movement overview — Godot Engine (stable) documentation in English ] (as far as I am aware)

This broken mess is what I’ve ended up with so far.


func get_input():

var InputDirection = Input.get_vector("MoveDown", "MoveUp", "MoveRight", "MoveLeft")
velocity = InputDirection * ScreenSpeed

func _process(delta):

var MSD : get_input() * ScreenSpeed * delta # I am aware this doesn't work

global_position += MSD


:wink: - I assume something along these lines is what you meant.

:rofl: - IT WORKS, YESSSSSSSSSS!!!

extends Camera2D

var InputActions: Array[String] = ["MoveDown", "MoveUp", "MoveRight", "MoveLeft"] #THIS IS USEFUL GET OVER IT

@export var ScreenSpeed := 500
func _process(delta: float):
	
	var InputVector : Vector2 = Input.get_vector("MoveLeft", "MoveRight", "MoveUp", "MoveDown")
	var velocity
	velocity = InputVector * ScreenSpeed * delta
	get_viewport_transform()
	position += velocity
	print(velocity)
	

:face_holding_back_tears: - This has brought me to tears and taught me a lot about do and do not’s.
Thank you all so much for the help and hopefully this helps someone else with this later.

Thank you. (I was unaware it wouldn’t trap me in my If block. Good to Know!!!) :grin:

func _process(dt):
	velocity = Input.get_vector("left", "right", "up", "down") * speed
1 Like

Thank you so much (I think this will bring me to my final script for this and finally finish solving my problem. Will Update post soon.)

1 Like