#my character x rotaions rotates for no reason while moving

24 messages · Page 1 of 1 (latest)

subtle willow
#

'''extends CharacterBody3D

const SPEED = 5
const JUMP_VELOCITY = 4.5
var walking = false

@onready var animation_player = $Visuals/player/AnimationPlayer
@onready var visuals = $Visuals
@onready var camera_point = $camera_point

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready():
GlobalMangerr.set_player(self)
animation_player.set_blend_time("idle","run",0.4)
animation_player.set_blend_time("run","idle",0.4)

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta

# Handle jump.
if Input.is_action_just_pressed("Accept") and is_on_floor():
    velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("west", "east", "north", "south")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
    velocity.x = direction.x * SPEED
    velocity.z = direction.z * SPEED
    
    visuals.look_at(direction + position)
    
    if !walking:
        walking = true
        animation_player.play("run")
else:
    velocity.x = move_toward(velocity.x, 0, SPEED)
    velocity.z = move_toward(velocity.z, 0, SPEED)
    
    if walking:
        walking = false
        animation_player.play("idle")

move_and_slide()

'''

pure lava
#

Is the transform.origin of your visuals at the same position as your CharacterBody3D?

If the visuals origin is on higher y position it might "look down" once you call visuals.look_at(direction + position)

pure lava
#

Global position

subtle willow
#

but when i make it zero it doesn't change anything

pure lava
#

If the visuals are "halve way into the ground" it still gets bent "downwards" ?

subtle willow
#

yes

pure lava
#

And if you move them further down, does it the angle change?

subtle willow
#

oh wait sorry if the visuals is half way down it works very well

pure lava
#

😊

subtle willow
#

yea sorry 😂

#

how to fix it then

pure lava
#

So one easy fix is to add the amount the visuals move up to your Y component of your look_at call. E.g. look_at(direction + position + Vector3(0, visuals.position.y, 0))

subtle willow
#

omg thank you so much it worked but i didn't understand what happened

pure lava
#

Okay, so what happens is twofold:

First, we expect the character (since it's a human model) to look straight "forward" from its eyes.

But in the game engine, the model could be anything, e.g. a ball. So it makes sense to use "the center" of objects, for your model that means most likely somewhere in its naval/tummy area.

This is why you needed to move the visual up by a certain amount, not to be stuck in the ground.

Second, the visuasl.look_at function gets fed the info from its parent (CharacterBody3D), but its parent position is "lower", it's more concerned with ground based movement not the visual representation.

If you use the parents information (e.g. position, direction) it will be more ground based. And the height of visuals needs to be incorporated in the calculations for the look_at to pick the correct "height" (y value) to look at, or else it will look at the ground. Since we rotate the whole model not only the head, it becomes even more apparent. If you would only rotate the head your character would always look down, keeping the body straight and it would have taken you a little longer to catch this.

subtle willow
#

thank you so much

subtle willow
#

@pure lava do you have any idea how to rotate the player to other directions smothly ?

pure lava
#

lerp() and slerp() are your friends. There should be some help in the documentation.

subtle willow
#

can't do it with look at?

subtle willow
pure lava
#

Try the F1 key in the editor and type in "lerp", it should show you different options one is also for Vector3 (which is 3D).