![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Denzeduous |
Hello everyone,
I’ve been having some issues trying to get hovering over a 3D object working. I currently have a node structure like:
Main
|>Camera
|>Player Hand
|-->Test Card
|---->MeshInstance
|------>StaticBody
|-------->CollisionShape
I attempted to try to use the signals from the StaticBody
to a script on the Test Card
to make the card grow in size (and shrink after hovering off) for when players are hovering over cards in their hands. However, this would not trigger… At all. I decided to do it manually and got it working, but in certain parts of the collision the entire thing begins to spaz out, growing and shrinking instantly between frames. I attempted a few different fixes, but now it’s spazzing out at every point on the card. I’m not quite sure what could be causing this and was wondering if anyone could point me in the right direction.
My code in the Player Hand
is as follows:
extends Node
var cards_distance = 15
var camera = null
var hand_node = null
var space_state = null
var last_card = null
var cards = []
func _ready():
camera = get_node("/root/Main/Camera")
hand_node = get_node("/root/Main/Player Hand")
space_state = hand_node.get_world().direct_space_state
func add_card(card):
cards.append(card)
# TODO: Generate card
func _process(delta):
var mouse_pos = get_viewport().get_mouse_position()
var from = camera.project_ray_origin(mouse_pos)
var to = from + camera.project_ray_normal(mouse_pos) * cards_distance
var card_collider = space_state.intersect_ray(from, to)
if !card_collider.empty():
var card = card_collider["collider"].get_node("../../")
if card != last_card:
if last_card != null:
last_card.off_hover()
card.on_hover()
last_card = card
elif last_card != null:
last_card.off_hover()
last_card = null
And the code for the Test Card
is:
extends Spatial
var hover = false
var hover_over = true
var seconds = 0.05
var seconds_passed = 0
var orig_size = Vector3()
var to_size = Vector3()
var orig_height = Vector3(0, 0, 0)
var to_height = Vector3(0, -30, 0)
func _ready():
orig_size = self.scale
to_size = self.scale * 7.5
func on_hover():
hover = true
hover_over = false
func off_hover():
hover = false
hover_over = true
seconds_passed = seconds - seconds_passed
func _process(delta):
if hover:
seconds_passed += delta
if seconds_passed > seconds:
self.scale = to_size
self.translation = to_height
seconds_passed = seconds
else:
self.scale = orig_size .linear_interpolate(to_size, seconds_passed)
self.translation = orig_height.linear_interpolate(to_height, seconds_passed)
else:
seconds_passed -= delta
if seconds_passed < 0:
self.scale = orig_size
self.translation = orig_height
seconds_passed = 0
else:
self.scale = to_size .linear_interpolate(orig_size, 1 - seconds - seconds_passed)
self.translation = to_height.linear_interpolate(orig_height, 1 - seconds - seconds_passed)
Any help would be appreciated. If I could simply get the StaticBody.mouse_entered
signal working, this would have been a lot easier… But alas, it was connected in the engine yet never fired once. Thank you for taking the time to read this, and I hope y’all are able to help point me in the right direction
EDIT: I have narrowed it down to the code in HandHover.gd
. Trying to figure out why exactly it stops detecting the card while hovering over it
EDIT2: Figured out I don’t need to use my HandHover.gd
code, just using a RigidBody
instead of a StaticBody