#convert local coordinate to global?

1 messages · Page 1 of 1 (latest)

kind vale
#
func _physics_process(_delta: float) -> void:
    if Input.is_action_pressed("forward"):
        move(-transform.basis.z.normalized())
    if Input.is_action_pressed("backward"):
        move(transform.basis.z.normalized())
    if Input.is_action_pressed("turn_left"):
        turn_left()
    if Input.is_action_pressed("turn_right"):
        turn_right()

func move(direction):
    #move the ray to the direction i'm going to
    $Ray.target_position = direction*2.5
    
    if !is_moving:
        #print(direction*2.5)
        
        #if path is blocked
        if $Ray.is_colliding():
            is_moving = true
            
            var col_point = $Ray.get_collision_point()
            var old_pos = position
            #var distance = position - col_point
            
            var tween = create_tween()
            tween.tween_property(self, "position", Vector3(col_point.x,position.y,col_point.z), 0.2)
            await tween.finished
            
            #play bump sound
            
            tween = create_tween()
            tween.tween_property(self, "position", old_pos, 0.2)
            await tween.finished
            
            move_timer.start()
            await move_timer.timeout
            is_moving = false
        
        #if path is clear
        else:
            SignalManager.movement.emit()
            is_moving = true
            
            var tween = create_tween()
            tween.tween_property(self, "position", position + (direction*MOVE_DISTANCE), 0.2)
            await tween.finished
            
            move_timer.start()
            await move_timer.timeout
            is_moving = false
#

hey so i'm trying to make a turn-based/grid-based movement. here is my current code, i can move and rotate all i want, now the issue i'm facing is i'm trying to detect if there is a collider in the direction i'm going to stop the character from just passing through the terrain using a raycast but it doesn't work rn, the raycast's direction changes based on my orientation(if my character is faced toward negative Z for example then it works, but on positive Z it's reversed, and on the X axis it just goes either left or right of my character). i think my problem is in the move fuction i use local coordinates, but the target_position uses global coordinates? i'm not really sure how to solve that, if it is even the problem. any ideas?

formal moss
#

i don’t know if this is the only issue, but make sure the ray is updated with force_raycast_update after changing it’s target_position

#

btw, target_position is a relative vector, so without seeing how direction is calculated it seems like it’s used correctly here.

kind vale
#

you can see in the _physics_process() function what direction is

#

i'll try and get a video to properly show the issue

#

here my character starts facing towards -Z (little white cube shows facing direction). so in this orientation going forward or backwards the raycast follows correctly, but you can see when i turn and move along the other axis the raycast doesn't work anymore

formal moss
#

after you apply the force_update?

kind vale
#

yes i applied it

formal moss
#

video isn’t playing for me. maybe discord is still transcoding…

#

is turn_left, turn_right, just a rotation?

kind vale
#

yeah it just rotates the player

#
func turn_left():
    if !is_moving:
        SignalManager.movement.emit()
        is_moving = true
        var tween = create_tween()
        tween.tween_property(self, "rotation:y", rotation.y + PI/2, 0.2)
        await tween.finished
        move_timer.start()
        await move_timer.timeout
        is_moving = false


func turn_right():
    if !is_moving:
        SignalManager.movement.emit()
        is_moving = true
        var tween = create_tween()
        tween.tween_property(self, "rotation:y", rotation.y - PI/2, 0.2)
        await tween.finished
        move_timer.start()
        await move_timer.timeout
        is_moving = false
formal moss
#

hmm, I’m guessing it is rotating the raycast too?

kind vale
#

it is yeah

formal moss
#

i think that might be the issue

#

you’re double-applying rotation

kind vale
#

yeah i've guessed this might be an issue but i have no idea how to prevent that then

formal moss
#

I think you should be able to simplify this quite a bit; just add a forward and backward raycast

#

like, $RayFwd, $RayBck

#

then there's no need to manually update anything

#

rotate the player as normal, then check the relevant raycast depending on the input

kind vale
#

alr yeah i guess that would be the simplest way

formal moss
#

I think that's closer to how the node was designed to be used, and how I'd do it.

If you want to control everything manually, I think you're better off going all the way and constructing the raycast on the fly, with something like get_world_3d().direct_space_state.intersect_ray(query_params)

kind vale
#

alright i ended up making a move function for each direction along with separate raycasts for them, way more bulky than what i had in mind but it works now so i'm not complaining lol