The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hello everyone,

I've been having some issues trying to get hovering over a 3D object working. I currently have a node structure like:

|>Player Hand
|-->Test Card

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):

    # 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 = card

    elif last_card != null:
        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

            self.scale       = orig_size  .linear_interpolate(to_size,   seconds_passed)
            self.translation = orig_height.linear_interpolate(to_height, seconds_passed)

        seconds_passed -= delta

        if seconds_passed < 0:
            self.scale       = orig_size
            self.translation = orig_height
            seconds_passed   = 0

            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 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 code, just using a RigidBody instead of a StaticBody

Godot version 3.4.2-stable_win64
in Engine by (16 points)
edited by

1 Answer

0 votes

Use a RigidBody instead of a StaticBody and you don't need to do any of this. You can simply use the mouse_entered and mouse_exited signals. Still buggy though, but now it doesn't have to do with the hovering and so I will open a different question.

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.