HELP plz !
Asked Answered
A

4

0

Hello,
i'm trying to code a top down space shooter in 3D (i did it in 2D)
I have problem to rotate the ship face to the mouse pos.
I dont want to use "look_at" but i need to use torque for realism.
I had try a lot of things 🙁
Thanks for your help.

Here my code

Ankylose answered 16/3 at 9:45 Comment(0)
A
0

Ankylose What behaviour are you trying to achieve?
From what I get, you either want the rotation to be smooth, in which case look_at is still the solution, you just need to apply it over time with a lerp or slerp.
Or you want the thing to over-rotate and correct itself going back.
If it's the latter, what you need to do is use a variable to tell which direction it has to rotate to align. At first I thought of a bool, but a float with -1.0-0.0-1.0 would be it.

var facing : float = (basis * Vector3.BACK).angle_to(target_pos)
if facing > 0.1:
     #rotate right
elif facing < 0.1:
     #rotate left
else:
     #aligned

I hope my code is correct, but you get the idea.

Aniakudo answered 16/3 at 16:25 Comment(0)
A
0

Aniakudo
thx for you reply, i will test what you said.

In fact, i want the same thing i made in the 2D version of my game :

target=get_global_mouse_position()
rotation_dir = transform.y.dot(position.direction_to(target))
set_constant_torque(rotation_dir * spin_thrust)

when you press UP, the ship go forward and rotate constantly to the mouse pointer.

Ankylose answered 16/3 at 20:13 Comment(0)
A
0

Aniakudo

your solution is the best way i think but your formula doesnt work.
I cant test it

Ankylose answered 17/3 at 16:1 Comment(0)
A
0

Ankylose sorry, I was on the phone when I wrote that code.
I tested this one and it works, though you need to test it with your game:
extends RigidBody3D


@onready var other = $"../MeshInstance3D"
@onready var label_3d = $"../Label3D"

func _physics_process(delta):
	var facing : float = (basis * Vector3.BACK).signed_angle_to(other.global_position, Vector3.UP)
	label_3d.text = str(facing)
	if facing > 0.4:
		constant_torque = Vector3(0.0, 8.0, 0.0)
		#add_constant_torque(Vector3(0.0, -0.08, 0.0))
	elif facing < -0.4:
		#add_constant_torque(Vector3(0.0, 0.08, 0.0))
		constant_torque = Vector3(0.0, -8.0, 0.0)
	else:
		constant_torque = Vector3(0.0, 0.0, 0.0)
Aniakudo answered 17/3 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.