Why is my character turning like this?

var vector: Vector3
var vector2: Vector3
var vector3: Vector3
var vector4: Vector3

func _process(_delta):
    $RayCast3D.position = -Vector3($RayCast3D.position.x, scale.y / 2 - 0.01, $RayCast3D.position.z)
    vector = $RayCast3D.get_collision_point()
    $RayCast3D2.position = -Vector3($RayCast3D2.position.x, scale.y / 2 - 0.01, $RayCast3D2.position.z)
    vector2 = $RayCast3D2.get_collision_point()
    $RayCast3D3.position = -Vector3($RayCast3D3.position.x, scale.y / 2 - 0.01, $RayCast3D3.position.z)
    vector3 = $RayCast3D3.get_collision_point()
    $RayCast3D4.position = -Vector3($RayCast3D4.position.x, scale.y / 2 - 0.01, $RayCast3D4.position.z)
    vector4 = $RayCast3D4.get_collision_point()
    $"../Node3D2".position = vector
    $"../Node3D2".look_at(vector2)
    $"../Node3D3".position = vector3
    $"../Node3D3".look_at(vector4)
    rotation = Vector3($"../Node3D3".rotation.x, $"../Node3D".rotation.y, $"../Node3D2".rotation.x)

the code is supposed to smoothly turn the character, which it is sorta doing by also turning it in the opposite angle.


22
23
the rotation.y part of the code doesn’t change the behaviour, it only turns the character based on the cameras angle.

any help adressing the topic helps, thank you.

Not sure what your math is trying to do. The rotation vector you give is pretty crazy and the Y value is part of the raycast-and-look manipulated process. It’s also pretty hard to see.

basically there are 4 raycasts, 1st behind the player, 2nd in front of the player, 3rd to the left and the 4th to the right. basically like a diagonal stool. i have 2 node 3d’s. the 1st node3d i place at the 1st raycasts collision point and turn it to look at the 2nd raycasts collision point. i do the same with the 2nd node3d, but with the 3rd and 4th raycasts. it for some reason just buffers the player like that.

image
smth like this, then i copy both of the node 3ds x rotation.

edit: by “look at 2nd/4th raycast” i mean collision points

I think I might kiiiinda understand the intention? You want the character to rotate when moving up a slope?

In your code, you are constantly flipping the raycasts’ x and z positions. That might be why your character is flipping back and forth. I’m thinking of lines like:

$RayCast3D.position = -Vector3($RayCast3D.position.x, scale.y / 2 - 0.01, $RayCast3D.position.z)

Where for example you put the raycast’s x position as the x component of the new vector, but then negate the whole thing.

1 Like

at the time of making the post, i tweaked the code and its running diffrently now. its smaller, so you wont have much trouble reading this.

func _process(_delta):
	var vector = $RayCast3D.get_collision_point()
	var vector2 = $RayCast3D2.get_collision_point()
	var vector3 = $RayCast3D3.get_collision_point()
	var vector4 = $RayCast3D4.get_collision_point()
	$"../Node3D2".position = vector
	$"../Node3D2".look_at(vector2)
	$"../Node3D3".position = vector4
	$"../Node3D3".look_at(vector3)
	rotation = Vector3($"../Node3D3".rotation.x, $"../Node3D".rotation.y, $"../Node3D2".rotation.x)

decided to reduce it to one slope, it doesn’t seem to work as intended either.


no scene changes.

ps: i might keep playing with this to see if i get anything new, just to lyk.

if this is true you should use the normal vector of the slope instead of the raycasts.

# on CharacterBody3D
move_and_slide()

var collision := get_last_slide_collision()
var normal := collision.get_normal()

# where the player will face, in this case where they are moving
var forward := velocity
if forward.length_squared() < 1:
    # or where they were looking, if not moving
    forward = transform.basis.z

look_at(forward + global_position, normal)