#Character Rotation

1 messages · Page 1 of 1 (latest)

brittle orchid
#

How do i rotate my character based on direction of movement?

stone ruin
#

First you define a Vector2 and call it something like "facingDirection" and initialize it looking at the right.
Then, after the part of the code where you register player input, check the angle between that two vectors. If it's bigger than a certain value, rotate the character, else make it translate

brittle orchid
#

its a 3D third person game btw

stone ruin
#

Ah and don't forget to update "facingDirection" if the character rotates

#

I have 0 experience with 3D sorry

brittle orchid
#

oh np

arctic carbon
#

From what I remember, if you can get the movement direction somehow, rotation.y = atan(move_dir.x, move_dir.z) will make the node's rotation so that it faces in that direction.

Maybe you can take velocity and normalize it and feed it in there.
You'll also need to check that the movement direction isn't Vector3.ZERO (standing still) or it'll throw an error.

brittle orchid
arctic carbon
brittle orchid
#

let me try atan2

arctic carbon
#

Oh yeah it's atan2 sorry my bad
I'll edit it

brittle orchid
#

movement and all that

#

should i give you script?

arctic carbon
#

Ahh yeah. the rotation.y part rotates whatever node the script it's on.

#

So if you give it some_other_node.rotate... etc etc it'll rotate that instead.

#

Depending on how your character is structured you might not want to rotate everything

#

You can put the script here sure

#

If we are lucky somebody smarter than me jumps in, too

brittle orchid
#

var mouse_sensitivity := 0.001
var twist_input := 0.0
var pitch_input := 0.0

@onready var twist_pivot := $TwistPivot
@onready var pitch_pivot := $TwistPivot/PitchPivot
@onready var animation_tree := $AnimationTree

var acceleration = 120.0

func _ready() -> void:
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _process(delta: float) -> void:
    var input = Vector3.ZERO
    input.x = Input.get_axis("left", "right")
    input.z = Input.get_axis("forward", "back")
    
    var direction = twist_pivot.basis * input
    if direction.length() > 0:
        velocity = direction.normalized() * acceleration * delta
    else:
        velocity = velocity.move_toward(Vector3.ZERO, acceleration * delta)
    
    move_and_slide()
    
    if Input.is_action_just_pressed("ui_cancel"):
        Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
        
    twist_pivot.rotate_y(twist_input)
    pitch_pivot.rotate_x(pitch_input)
    pitch_pivot.rotation.x = clamp(pitch_pivot.rotation.x, 
        deg_to_rad(-50), 
        deg_to_rad(20)
    )
    twist_input = 0.0
    pitch_input = 0.0
    
    var movement_direction: Vector3 = velocity.normalized()
    if movement_direction != Vector3.ZERO:
        rotation.y = atan2(movement_direction.x, movement_direction.z)
    
    animation_tree.set("parameters/conditions/idle", input == Vector3.ZERO)
    animation_tree.set("parameters/conditions/walk", input != Vector3.ZERO)
    

func _unhandled_input(event: InputEvent) -> void:
    if event is InputEventMouseMotion:
        if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
            twist_input = - event.relative.x * mouse_sensitivity
            pitch_input = - event.relative.y * mouse_sensitivity
brittle orchid
#

i dont think we need anybody else : )

arctic carbon
#

What if you add func _physics_process(): and put the code I gave inside it instead? Right now it's inside _process.

brittle orchid
#

okay ill try that

arctic carbon
#

I don't guarantee it changes much, but it'll then be synchronized with the physics system if nothing else.

brittle orchid
#

seems same

arctic carbon
#

Weird, maybe rotating the node is messing with the other nodes somehow, you seem to have all these twist pivots and stuff. Hm.

brittle orchid
arctic carbon
#

Oh I see

#

You could also try just having it rotate the visual part (armature) in case rotating the characterbody itself is breaking things.

#

It will probably face backwards if it works, though. Haha

brittle orchid
#

lol

#

what if i seperate camera3D?

#

with its script

#

i mean if it works can u help me code camera3d lol

#

or idk u tell any solutions

#

im stuck on this for hours lol

arctic carbon
#

I think it might work if you just export a variable for the Armature and do something like armature.rotation.y where it now says just rotation.y

brittle orchid
#

okay ill try

brittle orchid
arctic carbon
#

Haha

brittle orchid
#

xD

arctic carbon
#

Godot uses different forward directino for ndoes and meshes so you sometimes get 'em backwards like that. Happened to me before

#

Maybe you can just multiply the rotation by * -1 to reverse it

brittle orchid
#

sorry im totally new to this lol

arctic carbon
#

rotation.y = (atan2(movement_direction.x, movement_direction.z)) * -1

#

er armature.rotation

#

but yea jsut wrap it inside () and add * -1 and it will multiply the rotation by -1. Math stuff.

brittle orchid
#

wait

arctic carbon
#

I'm kinda bad at math so I'm not entirely sure if that's right yea

#

Sometimes jsut makign a rotation negative by making it have a minus sign in front can change results in a good way too

brittle orchid
#

ye

brittle orchid
#

behaving differently

#

i dont know how to explain lol so should i make a video of it so you can understand clearly?

arctic carbon
#

It might be turning as a mirror, like inverted in a way

brittle orchid
brittle orchid
arctic carbon
#

Okay

brittle orchid
arctic carbon
#

If the non-reversed one was working (but backwards) I think you could also just add a minus in front of "move_direction" so that it's actually facing opposite of move direction.

#

Though if you do you probably wanna rename that var lol, since it'll be the opposite of mvoement direction...

#

I just tested it in my own game... I know what you mean now

#

Doing * -1 on the atan2 makes it entirely mirrored, whoops.

brittle orchid
#

oh

arctic carbon
#

Try armature.rotation.y = atan2(-1 * movement_direction.x, -1 * movement_direction.z)

brittle orchid
#

-movement_direction.z

#

this worked

arctic carbon
#

Or that

brittle orchid
#

thank you

arctic carbon
#

Either one should work

brittle orchid
#

but not smooth

arctic carbon
#

Yeah 3d characters look super janky when rotated instantly

brittle orchid
#

anything to fix them?

arctic carbon
#

You can lerp angles lerp_angle(armature.rotation.y, THE FACE DIRECTION YOU WANT, delta * LERPING SPEED)
I put examples just in caps there lol

#

I think you could modify the script so that instead of applying that rotation immediately you store it in a var

#

Then lerp to it over time

brittle orchid
#

please tell me what to write where 🥲

#

and btw

#

the camera isnt free

#

like i cant freely move the camera while moving

arctic carbon
#

Yeah sure. lerp means linear interpolation. It basically smoothly moves a value from one point to another.
Can you paste the part of the code in _physics_process?
I'd rather have the exact same version you have before I try modifying it

brittle orchid
arctic carbon
#

Ah yeah I'm not sure aboit twist pivots ... I've actually never used them.

brittle orchid
arctic carbon
#

I meant the whole physics_rpocess thing, but I think I have a good guess on how exactly it looks atm

brittle orchid
#
    var movement_direction: Vector3 = velocity.normalized()
    if movement_direction != Vector3.ZERO:
        armature.rotation.y = (atan2(movement_direction.x, -movement_direction.z)) * -1 
        lerp_angle(armature.rotation.y, 1, delta * 1)```
#

how do i write this lerp thing bro

arctic carbon
#
    var movement_direction: Vector3 = velocity.normalized()
    if movement_direction != Vector3.ZERO:
        var final_rotation_target = (atan2(movement_direction.x, -movement_direction.z)) * -1 
        armature.rotation.y = lerp_angle(armature.rotation.y, final_rotation_target, delta * 20.0)```

I haven't tested it, I'm not 100% sure if that ``final_rotation_target`` part needs to be stored somehow.
#

In my game I jsut had 2 nodes, one rotates isntantly and the one with the armature lerps after it's rotation.

#

Edited, sorry I left one of the 1s in there by accident

brittle orchid
#

np

#

thank you!! its much better now

#

one more thing, it rotates very fast when we press any movement button

#

wait

arctic carbon
#

Yeah and you probably figured but the 20.0 is the speed it turns the mesh at

#

I mean armature but yeah models/meshes following armature

brittle orchid
#

oh

arctic carbon
#

You could make the 20.0 into a var or just fiddle with it directly.
I made the character in my game turn super fast but just slow enough that it doesn't look super janky

brittle orchid
#

ye thanks

#

i did 10.0 it looks pretty nice

arctic carbon
#

Yea 10.0 probably nicer in a more realistic style game

#

I have one warning I learned the hard way btw

#

Lerping the mdoels liek that looks fine. But if oyu ever lerp like a uh... real rotation

#

And you want the character shooting stuff in the rotation

#

It can make controls feel really bad, because it will fire in the direction the armature is facing atm.

brittle orchid
#

oh

arctic carbon
#

That's why I use thei sntant one for facing for actual attacks and jsut lerp the armature for visuals

#

I just made my "real facing" a separate child node because all the rotations made my head hurt.
There's probably osme smarter way to handle it all but I liek keeping things easy

brittle orchid
#

i saw one tutorial he did seperate script for camera3d and and it wasnt under character, he made it what i want but it was too buggy and too bad so i didnt do that

brittle orchid
#

i want free camera while moving, but ig we cant do that with this

#

but honestly rn it looks much better

#

even for free camera too

arctic carbon
#

As for the camera. I used a free add-on called PhantomCamera for my game.
It has bit of a learning curve... and in current version it's very jittery when above 60FPS because of a feature that isn't in Godot yet.
But I just didn't want to do all the math involved with good cameras.

So I can't help with that... but I'm glad your dude is rotating now.

brittle orchid
#

so i dont need it rn

#

not planning to make like those action games so ig we're good

#

@arctic carbonthank you so much bro, you helped me a lot. can i disturb you again if i want more help later sometime? ( im just started and im so noob so obv i would need more help lol sry)

arctic carbon
#

I'm often focused on something. But I read newbie threads pretty often. Quick-help ch, too.

brittle orchid
arctic carbon
#

No offense but I don't really want people DMing me. We cool, though!