+1 vote

Im on the version 3.2.1 of godot, for what i have seen on tutorials in order to code my kinematicbody and move it, i need getglobalmouseposition but when i wrote it the progam doesn't recognize that code. Instead i think i need to use globaltransform but i don't know how to use it.

What other method could i use to move a KBody3D with a mouse? if you can provide the code it would be awesome.

in Engine by (13 points)

what camera view is this? Is it a top down? Third person?

Im a newbie so, i think it is called Eagle View, from the sky, but if im not mistaken it is the same as Third Person but from a higher perspective? if that's the case what should i do?

What tutorial are you following? Have you checked the 3d navmesh template? It has a point and click system for movement. It uses rays cast from the camera to calculate where has been clicked.

This is one of the tutorials i have been following: https://www.youtube.com/watch?v=_urHlep2P84
And this from the Godot Manual: https://docs.godotengine.org/en/stable/tutorials/2d/2d_movement.html#click-and-move
Some of the code lines doesn't work on 3D so im a bit confused

I'm aware of the point and blick system on 3Dnavmesh but im not sure how to to link the signal that the RayCast emits and link it to the KinecBody in order to make it move

Do you have an example of the code you're using? I know some things are very different between 2D and 3D, I mostly work in 3D, as that's just my preference.

From what I remember, the navmesh example doesn't use a signal, it uses the raycasts to get a position, which it then uses. But the navmesh example uses a master code to run the scene. I used it to figure out how to create enemies which use a navmesh for movement from within their own code. I haven't used it for a point and click system.

I think there is (or was) a point and click add-on for Godot. Esporia I think.

The Godot 3D point and click framework is Escoria (I was so close).

https://github.com/godotengine/escoria

I haven't tried it myself, but it may have something you find useful.

This is the exact code I've been using so far

extends KinematicBody

var maxspeed = 400
var speed = 0
var move
direction
var acceleration = 1200
var moving = false
var destination = Vector3()
var movement = Vector3()
var position = Vector3(0, 0, 0) # ?

func unhandledinput(event):
if event.isactionpressed('Botonderecho'):
moving = true
destination = global
transform # here is wherei had the most trouble so far

func physicsprocess(delta):
MovementLoop(delta)

func MovementLoop(delta):
if moving == false:
speed = 0
else:
speed += acceleration * delta
if speed > maxspeed:
speed = max
speed
movement = position.movedirection(destination) * speed
move
direction = rad2deg(destination.angleto(destination)) #angleto doesnt work on 3.2.1
if position.distanceto(destination) > 5:
movement = move
and_slide(movement)
else:
moving = false

For this code I followed a 2D tutorial and changed some lines with I know so far to be able to work on 3D
This is the video where i saw it: https://youtu.be/05OixHPbxNA

I will check it (y)

1 Answer

+2 votes

I knocked up a quick demo based on your code.
It contains 2 versions, with and without a navmesh.
There is some code which doesn't need to be there, as I reused it from other projects.
Hope it helps.

To get the 3D mouse click position, I use camera raycasts for normal and origin, and then call a raycast between those two points, getting the 3D position of that intersect as the move target.

The demo project can be found here.
https://github.com/DamonRaziel/Godot-Click-and-Move-Demos

The modified code I used was:

extends KinematicBody

var maxspeed = 400
var speed = 0
var movedirection
var acceleration = 120
var moving = false
var destination = Vector3()
var movement = Vector3()
var position = Vector3(0, 0, 0) # ?

# added vars
var player_position = Vector3()
var marker_position = Vector3()
var move_speed = 15.0
var slow_speed = 3.0
var travel_speed = 5.0
var velocity = Vector3()
var gravity = 0.0

func _input(event):
#func _unhandled_input(event):
#   if event.isactionpressed('Botonderecho'):
#       moving = true
        #destination = globaltransform # here is wherei had the most trouble so far
    if (event.is_class("InputEventMouseButton") and event.button_index == BUTTON_LEFT and event.pressed):
        var from = get_parent().get_node("Cam").project_ray_origin(event.position)#*100

        var to = from + get_parent().get_node("Cam").project_ray_normal(event.position)*100
        var space_state = get_world().direct_space_state
        var result = space_state.intersect_ray(from, to, [self], collision_mask)
        print ("RESULT : ", result.position)
        moving = true
        destination = result.position

func  _physics_process(delta):
    move_to_marker(destination, delta)
#   MovementLoop(delta)

#func MovementLoop(delta):
#   if moving == false:
#       speed = 0
#   else:
#       speed += acceleration * delta
#   if speed > maxspeed:
#       speed = maxspeed
#       movement = position.direction_to(destination) * speed #move_direction(destination) * speed
#       movedirection = rad2deg(destination.angle_to(destination)) #angleto doesnt work on 3.2.1
#   if position.distance_to(destination) > 5:
#       movement = move_and_slide(movement)
#   else:
#       moving = false

func move_to_marker (marker, delta):
    player_position = self.get_global_transform().origin
    marker_position = marker
    var offset = Vector3()
    offset = marker_position - player_position
    var dir = Vector3()
    dir += offset
    dir = dir.normalized()
    velocity.y += delta*gravity
    var hv = velocity
    var target = dir
    var attacking = false
    target *= move_speed
    var ATTACK_ACCEL = 1
    var SPRINT_ACCEL = 2
    var accel
    if dir.dot(hv) > 0:
        accel = travel_speed
    else:
        accel = slow_speed
    hv = hv.linear_interpolate(target, accel*delta)
    velocity.x = hv.x
    velocity.z = hv.z
    velocity.y = hv.y
    velocity = move_and_slide(velocity, Vector3(0,1,0))
by (106 points)

Thank you so much!!

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 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 webmaster@godotengine.org with your username.

Categories