Making the player orbit around a point using input direction

Godot Version

4.5

Question

Hello, trying out 3d for the first time and struggling a bit.
I want my player to orbit around a fixed target point. Currently it is slowly spiraling away from the point because velocity dosent account for curvature. As it is the player is just moving to the side which slowly moves the player away from the fixed point. The player should always maintain the same distance to the fixed point

I found a video that explained it using this graphic although i cant seem to get it to work

func target_locked_dir() -> Vector3:
	var c = character # player 
	var cam = c.camera_manager # current target is in this script
	var input_dir = Input.get_vector("sideways_left","sideways_right","forward","backward",)

	var forward 
	var right_curved 

	var vec3_to_target : Vector3 = (c.position - cam.current_target.position).normalized()

# as it is right now  direction DOES NOT  account for curvature 
	right_curved = cam.camera_arm.global_basis.x 
	forward = cam.camera_arm.global_basis.z

	var direction = input_dir.x * right_curved + input_dir.y * forward

	return direction

I’ve tried a bunch of things to get this setup to work
Should i follow a different tutorial? or how do i understand this one

Any

I think will be simpler if you move the camera following the circle and looking at the center as target.

1 Like

I think i already am? the camera is always facing the target point.
If you mean to literally move the camera wouldnt i run into the same issue? (figuring out the where to place the camera)

The circle isnt a literal circle that the player (or the camera) moves along. The desired result is for the player to, when moving sideways, run around the fixed point as if following a circles line, e.g. orbiting.
Sorry if I’m misunderstanding what you are saying

If the player rotation is locked to always face the target, and you always move the player to its right vector, it will end up moving around the target in a circle, no? Would that not work?

No, it ends up spiraling instead of moving in a circle. At the exact moment the player is moved sideways, the player dosent follow the circle but the vector directly to the right or left of the player (see black arrow marked with orbiting). The desired result should that vector, but rotated slightly so that it follows the circle (see green arrow - orbiting curved)

1 Like

Sorry but from the comment in your code, looks like the movement is linear. If you move based on the actual direction and then you change direction, you move on an spiral.
Calculate the position first based on time and speed keeping the distance to the target, update the camera/player position and after that align it with the target.

1 Like

yes, that is want I want to do. I’m having trouble figuring out how to calculate it. I can’t seem to get it to work from the example in the video.
I don’t know how to calculate it

Sorry. If you will move on a plane, like X/Z, you can use sine and cosine to rotate around Y.
Like x = x_center + radius x cos(angle_radians)
z= z_center + radius
x sin(angle_radians)
Where x_center, z_center are the target position x/z and
angle_radians = angle_radians + delta x rotation_speed x input_dir.x
That should move the object around a circumference.

2 Likes

Thank you :slight_smile:
I got it working!

2 Likes

Awesome!!!

1 Like