- Edited
Hello, I am learning Godot.
I am trying to make post-apocalyptic, building game. I am following a bunch of tutorials. Currently I have semi-working camera movements. Second step was to make tilemap, where first level is ground, second level are buildings which will be built, and third level are ghost buildings upon placing.
However, I have an issue where tiles under cursor are wildly wrong, ghost tile is far away after moving camera out of the initial viewport area. Below, you will find video to explain it better.
Here is my camera code
extends Camera2D
var Tick = 0
var SPEED = 20.0
var ZOOM_FACTOR = 1.0
var ZOOM_POS = Vector2()
var ZOOMING = false
var ZOOM_SPEED = 20.0
var ZOOM_MARGIN = 0.1
var ZOOM_MIN = 0.5
var ZOOM_MAX = 3.0
var EDGESCROLL_THRESHOLD = 20
func _input(event):
ProcessZoom(event)
ProcessMove(event)
func ProcessZoom(event):
if abs(ZOOM_POS.x - get_global_mouse_position().x) > ZOOM_MARGIN:
ZOOM_FACTOR = 1.0
if abs(ZOOM_POS.y - get_global_mouse_position().y) > ZOOM_MARGIN:
ZOOM_FACTOR = 1.0
if event is InputEventMouseButton:
if event.is_pressed():
ZOOMING = true
if event.is_action("WheelDown"):
print("ZOOM OUT")
ZOOM_FACTOR -= 0.01 * ZOOM_SPEED
ZOOM_POS = get_global_mouse_position()
if event.is_action("WheelUp"):
print("ZOOM IN")
ZOOM_FACTOR += 0.01 * ZOOM_SPEED
ZOOM_POS = get_global_mouse_position()
else:
ZOOMING = false
func MoveCamera(InputX, InputY):
position.x = lerp(position.x, position.x + InputX * SPEED*zoom.x, SPEED*Tick)
if position.x < 0:
position.x = 0
position.y = lerp(position.y, position.y + InputY * SPEED*zoom.x, SPEED*Tick)
if position.y < 0:
position.y = 0
func ProcessMove(event):
if event is InputEventKey:
if event.pressed:
if (event.keycode == KEY_RIGHT or event.keycode == KEY_D):
MoveCamera(1.0, 0.0)
elif (event.keycode == KEY_LEFT or event.keycode == KEY_A):
MoveCamera(-1.0, 0.0)
elif event.keycode == KEY_UP or event.keycode == KEY_W:
MoveCamera(0.0, -1.0)
elif event.keycode == KEY_DOWN or event.keycode == KEY_S:
MoveCamera(0.0, 1.0)
func _process(delta):
Tick = delta
zoom.x = lerp(zoom.x, zoom.x * ZOOM_FACTOR, ZOOM_SPEED*Tick)
zoom.y = lerp(zoom.y, zoom.y * ZOOM_FACTOR, ZOOM_SPEED*Tick)
zoom.x = clamp(zoom.x, ZOOM_MIN, ZOOM_MAX)
zoom.y = clamp(zoom.y, ZOOM_MIN, ZOOM_MAX)
if not ZOOMING:
ZOOM_FACTOR = 1.0
var ViewPortSize = get_viewport().size
var MousePosition = get_viewport().get_mouse_position()
if ((ViewPortSize.x - MousePosition.x) < EDGESCROLL_THRESHOLD):
MoveCamera(1.0, 0.0)
elif MousePosition.x < EDGESCROLL_THRESHOLD:
MoveCamera(-1.0, 0.0)
if ((ViewPortSize.y - MousePosition.y) < EDGESCROLL_THRESHOLD):
MoveCamera(0.0, 1.0)
elif MousePosition.y < EDGESCROLL_THRESHOLD:
MoveCamera(0.0, -1.0)
And here is the building code
extends Node2D
var SelectedToBuild = false
var SelectedObjectForBuilding = null
var CreatedSpriteGhost : Sprite2D = null
var LastCellPreview = null
var PreviewLayer = 2
@onready var Ground : TileMap = get_tree().get_root().get_node("Game").get_node("Ground")
func _ready():
pass
func _input(event):
if event is InputEventKey:
if event.pressed:
if event.keycode == KEY_ESCAPE:
pass
func _physics_process(delta):
if LastCellPreview:
Ground.clear_layer(PreviewLayer)
LastCellPreview = null
if SelectedToBuild:
if not CreatedSpriteGhost:
var mouse_pos = get_viewport().canvas_transform.affine_inverse().basis_xform(get_viewport().get_mouse_position())
var tile_mouse_pos = Ground.local_to_map(mouse_pos)
print(tile_mouse_pos)
Ground.set_cell(PreviewLayer, tile_mouse_pos, 3, Vector2i(1, 3))
LastCellPreview = tile_mouse_pos
else:
pass
else:
pass #remove ghost
Here is the link to video, cant upload over 2mb:
Thank you very much in any way