Hi everyone, I seem to be having a problem that I can't really resolve, can I get some help?
My goal is to make my rig move around other AI and Bodies in the scene.
However, whenever my rig moves at another rig on the same layer and touches it, it crashes the game. Seems odd, because the move_and_slide (of KinematicBody) method says it should slide around another Body.
Here is the code, it's not too long, but includes ways of making raycast check on objects from the camera, and a marker to mark the location. Other than that I kind of brute-forced the ability to more accurately end movement exactly on the origin of the place I clicked, instead of "kinda" close to it, by using counters. Everything worked just fine solo, but as soon as I added another object, and the rig touches it it crashes the game!
This is the only script I have in my entire game so far. I'm trying to resolve this physics/body issue before moving forward.
Aside from that I just have a floor, and an object that the Player rig interacts with.
Am I really missing something? Something special about the way move_and_slide works and when it chooses to interact with things? I have a basic understanding of layers and masks, as well. If the code is too much to read, perhaps someone can at least give me some clues to working better with move_and_slide. Thank you!!
(I've also included images of the two scenes involved)
extends KinematicBody
onready var camera = $Camera
var speed = 1.5
var velocity = Vector3.ZERO
var position = Vector3.ZERO
var counter = 0
var counter2 = 0
var my_counter = false
var my_vec = Vector3( -1, 5, -1 )
func _ready() -> void:
$Marker.hide()
func _process(_delta: float) -> void:
pass
func _physics_process(_delta):
# Confine Mouse Cursor to Window
Input.set_mouse_mode( Input.MOUSE_MODE_CONFINED )
if Input.is_mouse_button_pressed( BUTTON_LEFT ):
# Store the "Space"
var space_state = get_world().direct_space_state
# Store the Mouse's current position (Screen Point)
var mouse_position = get_viewport().get_mouse_position()
# Store the Camera's Ray Origin
var ray_origin = camera.project_ray_origin( mouse_position )
# Store the End of the Camera's Ray Normal * 2000 and added to the Ray Origin
var ray_end = ray_origin + camera.project_ray_normal( mouse_position ) * 2000
# Store the Dictionary of the intersection of Ray's Origin & Ray's End
var ray_hit = space_state.intersect_ray( ray_origin , ray_end, [], 0b00000000000000000001 )
# Look at Mouse Position Based on Ray Intersection
if ray_hit:
# print( "Hit at point: ", ray_hit.position )
$Rig.look_at( Vector3( ray_hit.position.x , translation.y, ray_hit.position.z ) , Vector3.UP )
velocity = -$Rig.transform.basis.z * speed
position = ray_hit.position
$Marker.show()
my_counter = false
counter2 = 10
# print ( $Rig.global_transform.origin.distance_to( position ) )
# This if-block makes for a smooth landing to the click position if it's inside .5 distance_to
if $Rig.global_transform.origin.distance_to( position ) >= .4 && $Rig.global_transform.origin.distance_to( position ) < .5:
counter2 = 10
elif $Rig.global_transform.origin.distance_to( position ) >= .35 && $Rig.global_transform.origin.distance_to( position ) < .4:
counter2 = 9
elif $Rig.global_transform.origin.distance_to( position ) < .35:
counter2 = 7
# This and the counter system prevents the Rig from over shooting the click position
if $Rig.global_transform.origin.distance_to( position ) < .5:
velocity = Vector3.ZERO
my_counter = true
if my_counter:
counter2 -= 1
if counter2 > 0:
velocity = -$Rig.transform.basis.z * speed
if counter2 <= 0:
my_counter = false
velocity = Vector3.ZERO
velocity = move_and_slide(velocity, Vector3.UP )
$Marker.global_transform.origin = position
if $Marker.visible == true:
counter += 1
if counter >= 80:
counter = 0
$Marker.hide()

