- Edited
I have a cube that follows my mouse cursor but for some reason it flickers and disappears every now and then on the screen (see recording)
extends Node3D
var mouse: Vector2
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _input(event):
# if event is InputEventMouse:
# mouse = event.position
# if event is InputEventMouseButton and event.is_pressed():
# if event.button_index == MOUSE_BUTTON_LEFT:
# get_selection()
if event is InputEventMouseMotion:
mouse = event.position
get_selection()
func get_selection():
#Boiler plate code to figure out where the cursor is pointing at
var worldspace= get_world_3d().direct_space_state
var camera = $Camera3D
var start = camera.project_ray_origin(mouse)
var end = start + camera.project_ray_normal(mouse) * 1000
var query = PhysicsRayQueryParameters3D.create(start, end)
query.exclude = [$StaticBody3D2]
query.collide_with_areas = true
var result = worldspace.intersect_ray(query)
# print(result)
if(result):
var pos = Vector3(result.position)
#snapper is the size of the my grid that I want the cube to snap to
var snapper = Vector3(1,1,1)
# https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#class-globalscope-method-snapped
# https://docs.godotengine.org/en/stable/classes/class_vector3.html#class-vector3-method-snapped
var snappedPos = pos.snapped(snapper)
# print(snappedPos)
var cube = $StaticBody3D2
if(cube.global_position != snappedPos):
cube.global_position = snappedPos
Any ideas what that might be?
Max
EDIT: as per @award comment, I have removed the ZIP file and replaced it with a YT video instead