#Character Rotation
1 messages · Page 1 of 1 (latest)
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
its a 3D third person game btw
Ah and don't forget to update "facingDirection" if the character rotates
I have 0 experience with 3D sorry
oh np
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.
im totally new to this so i cant understand lol, can i give you my script + more details in DM to help me? its fine if you dont want to
You could try putting these in _physics_process:
if movement_direction != Vector3.ZERO:
rotation.y = atan2(movement_direction.x, movement_direction.z)```
I'm not 100% sure it works, but I feel like it should.
Too many arguments for "atan()" call. Expected at most 1 but received 2.
let me try atan2
Oh yeah it's atan2 sorry my bad
I'll edit it
its rotating but everything is like messed up
movement and all that
should i give you script?
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
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
u can do it, ur very smart
i dont think we need anybody else : )
What if you add func _physics_process(): and put the code I gave inside it instead? Right now it's inside _process.
okay ill try that
I don't guarantee it changes much, but it'll then be synchronized with the physics system if nothing else.
seems same
Weird, maybe rotating the node is messing with the other nodes somehow, you seem to have all these twist pivots and stuff. Hm.
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
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
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
okay ill try
u were right its worked but facing backwards
Haha
xD
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
where should i write that?
sorry im totally new to this lol
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.
its now behaving differently
wait
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
ye
i did exactly like that
behaving differently
i dont know how to explain lol so should i make a video of it so you can understand clearly?
It might be turning as a mirror, like inverted in a way
wait ill show u
Okay
uh i gotta download screen recorder first sry lol
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.
Try armature.rotation.y = atan2(-1 * movement_direction.x, -1 * movement_direction.z)
Or that
thank you
Either one should work
but not smooth
Yeah 3d characters look super janky when rotated instantly
anything to fix them?
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
please tell me what to write where 🥲
and btw
the camera isnt free
like i cant freely move the camera while moving
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
Ah yeah I'm not sure aboit twist pivots ... I've actually never used them.
do u mean this ? lerp_angle(armature.rotation.y, THE FACE DIRECTION YOU WANT, delta * LERPING SPEED)
I meant the whole physics_rpocess thing, but I think I have a good guess on how exactly it looks atm
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
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
np
thank you!! its much better now
one more thing, it rotates very fast when we press any movement button
wait
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
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
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.
oh
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
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
hmm
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
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.
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)
I'm often focused on something. But I read newbie threads pretty often. Quick-help ch, too.
don't worry im gonna ask first if you're free, can i add you ?
No offense but I don't really want people DMing me. We cool, though!