How do you use a Static Body for a 3D Title Screen? [ANWSERED]

KindosaurGamingKindosaurGaming Posts: 24Member
edited December 4 in Programming

Hello to anyone reading my post,

Is there a way to make a 3D title screen using a StaticBody, I have already made some progress in it but I came across a problem.

here is the code I use for my 3D title screen

extends StaticBody

func _ready(): connect("input_event", self, "on_input_event")
func on_input_event(camera, event, click_position, click_normal, shape_idx): var mouse_click = event as InputEventMouseButton if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed: print("clicked") get_tree().change_scene("res://Game/Scenes/World.tscn");

What happens is the following the title screen scene loads (I see my animations and stuff) and when I hover my mouse over the New Game (it doesn't require a mouseinput) it already starts.

what I want is that when my mouse hovers over the New Game it wont directly start the selected scene but waits until you have made the input (Leftclick).

My game is in its early stages so my title screen looks terrible but it I do want it to work


Tags :

Best Answer

  • cyberealitycybereality Posts: 30
    Accepted Answer

    Actually, I tried your code and it worked. Maybe you had a typo somewhere (I see a semicolon on the last line, maybe that was it, or incorrect indentation).

    extends StaticBody
    
    func _ready(): 
        connect("input_event", self, "on_input_event")
    
    func on_input_event(camera, event, click_position, click_normal, shape_idx): 
        var mouse_click = event as InputEventMouseButton 
        if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed: 
            print("clicked") 
            get_tree().change_scene("res://World.tscn")
    

Answers

  • cyberealitycybereality Posts: 30Member
    edited December 4

    You can do this with a ray cast. Here is some code I found on Reddit that I tested and works.

    const RAY_LENGTH = 10
    
    func _physics_process(delta):
        var object = get_object_under_mouse()
        if object:
            print("Object Name: ", object.collider.name)
        else:
            print("Not Over Object")
    
    
    func get_object_under_mouse():
        var mouse_pos = get_viewport().get_mouse_position()
        var ray_from = $Camera.project_ray_origin(mouse_pos)
        var ray_to = ray_from + $Camera.project_ray_normal(mouse_pos) * RAY_LENGTH
        var space_state = get_world().direct_space_state
        var selection = space_state.intersect_ray(ray_from, ray_to)
        return selection
    
  • cyberealitycybereality Posts: 30Member
    Accepted Answer

    Actually, I tried your code and it worked. Maybe you had a typo somewhere (I see a semicolon on the last line, maybe that was it, or incorrect indentation).

    extends StaticBody
    
    func _ready(): 
        connect("input_event", self, "on_input_event")
    
    func on_input_event(camera, event, click_position, click_normal, shape_idx): 
        var mouse_click = event as InputEventMouseButton 
        if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed: 
            print("clicked") 
            get_tree().change_scene("res://World.tscn")
    
  • KindosaurGamingKindosaurGaming Posts: 24Member

    Thank you @cybereality it works like a charm

  • cyberealitycybereality Posts: 30Member

    @KindosaurGaming said:
    Thank you @cybereality it works like a charm

    Awesome! So glad I could help.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file