#How to make motion relative to camera

25 messages · Page 1 of 1 (latest)

pliant raft
#

Hi, I'm currently working on a 2dhd game. My character controller uses a standard script with some tweaks to manage the AnimatedSprite3D animations.
I'm having trouble on making the movement relative to camera, rather than the whole space itself: when the player presses Q or E keys, the camera rotates of 45deg (left or right), though the player doesn't change it's direction as relative to it
What would be the best way of doing that?

this is my current player movement function (the auxiliary functions are just to update the animations based on the input vector direction)

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

    if not InputManager.isinputblocked:
        var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
        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
            update_animation(input_dir)
        else:
            velocity.x = move_toward(velocity.x, 0, SPEED)
            velocity.z = move_toward(velocity.z, 0, SPEED)
            update_idle_animation() 
    
    move_and_slide()

foggy sapphire
# pliant raft Hi, I'm currently working on a 2dhd game. My character controller uses a standar...

While moving mouse, for left, right rotate the characterbody node only not the camera, camera is a child so it will follow up and for up down rotate the camera. That way character node is always facing forward with respect to camera and the forward is always the forward direction and velocity is relative to the characterbody's local coord. For using WASD, don't move the characterbody but rotate the mesh. Then forward is forward direction, left is left and vice versa.

pliant raft
pliant raft
#

Ok, will try it! thanks!

foggy sapphire
foggy sapphire
pliant raft
#

so if the whole characterbody rotates then the camera will do it too, and it doesn't interfere with the input vector right?

pliant raft
#

Okok, thanks a lot then

foggy sapphire
#

Since camera is a child of characterbody so it will follow it, whatever it does and wherever it goes

pliant raft
#

Do you know if it changes something with the phantomcamera add-on?

#

i was thinking of using that in future

foggy sapphire
#

No

#

It won't cause any problem

#

Phantom camera is just a camera module with a bunch of functions. To provide extra features. If you don't use those then its just a normal camera

pliant raft
#

got it, thanks again

foggy sapphire
# pliant raft got it, thanks again

Also, I think I should explain this too so you don't have to look for it later in the future. But this approach will make your character always rotate as long as camera is rotated and always show its back to the camera and look in front but in some games where there is an effect where the camera rotate but the character doesn't, like in genshin so you can look you characters from the front but they do follow the direction if you start moving. For that effect you'll need to add an extra logic that the mesh maintains its current active forward direction in global coords until wasd overwrite its and give it nee forward direction in global direction which it will use later when it stops. So while moving do nothing, when stopped registered global facing direction of mesh and keep it until starts moving again. That will take care of the effect

pliant raft
#

does this problem occour also with sprites3d with y billboard enabled?

foggy sapphire
#

You win't need this

pliant raft
#

phew

#

I'll be checking out stairs later

foggy sapphire
#

I meant like in pubg when you look around the character also looks around always shows its back to your, until you use the eye button to use free look