• Godot HelpProgramming
  • Rotation gets messed up the moment i pass to the other side of coordinate 0. What's going on?

I tried to adapt this code to my situation. The rotation works as intended while spawning on this side, looking up works, looking down works: But not when going past the blue axis or spawning in that area. There, the z axis rotation (up-down) gets reversed for some reason that my walnut brain cannot comprehend. It's most likely related to the z axis rotation, but not sure what. Can someone point out what needs changing, please?

I'm guessing unlike Game Maker, Godot doesn't consider the coordinates of the object I've attached the script to. But not sure.

EDIT: Fixed it by adding the name of the mesh before the .rotate_z(deg2rad(change)) but now I cannot go downward-upward with W and S if I point the mouse up or down. This is the code used: var aim = get_global_transform().basis if Input.is_action_pressed("forward"): direction += aim.x if Input.is_action_pressed("backward"): direction -= aim.x It allows going forward / backward, but doesn't go forward/backward based on up-down rotation anymore if I tell it to use the mesh's rotate_z

So you are trying to rotate the object towards the mouse?

@SIsilicon28 said: So you are trying to rotate the object towards the mouse?

With the mouse. Kind of like how games with spaceships or swimming (see Subnautica for example) work. So far all of my controls work for strafing left/right, going up/down without looking, and going forward/backward while looking into any direction in the horizon. But I can no longer look up/down and go that way with forward/backward unless I remove the mesh's name before the rotate_z. But if I remove the name, it starts flipping the rotation axis. Here's a video of what the problem is. It goes forward toward the horizon instead of downward where I point the mouse.

@cybereality said: See this tutorial for how it is done.

https://kidscancode.org/godot_recipes/3d/camera_gimbal/

Thanks, but I'm not sure how to adapt that one to my situation. I have a mesh that has a camera and a light attached to it. I want the mesh to both rotate with mouse motion and be able to travel in the direction it's facing by pressing the forward button.

And if I'd try to apply that code to the kinematic body, I get an error about trying to apply local rotation on a null instance. Does it have to (and can it) be applied to the mesh instead?

This is the old script I'm using for the situation in the video in my prior post:

extends KinematicBody

var cam_angle = 0
var mouse_sensitivity = 0.3

var velocity = Vector3()
var direction = Vector3()

var shift = 1
var moving = 0

const FLY_SPEED = 30
const FLY_ACCEL = 1

func _ready():
	pass


func _physics_process(delta):
#reset direction
	direction = Vector3()
#get camera rotation
	var aim = get_global_transform().basis

#CONTROLS
	if Input.is_action_pressed("accelerate") && shift < 4 :
	   shift += 1
	if Input.is_action_pressed("accelerate") && !Input.is_action_pressed("up") && !Input.is_action_pressed("down") && !Input.is_action_pressed("forward") && !Input.is_action_pressed("backward") && !Input.is_action_pressed("right") && !Input.is_action_pressed("left"):
	   shift = 1
	if !Input.is_action_pressed("accelerate") && shift >1:
	   shift -= 0.1
	if shift < 1:
		shift = 1
	

	if Input.is_action_pressed("forward"): 
		direction += aim.x
	if Input.is_action_pressed("backward"): 
		direction -= aim.x

	if Input.is_action_pressed("left"): 
		direction -= aim.z
	if Input.is_action_pressed("right"): 
		direction += aim.z
	if Input.is_action_pressed("up"): 
		direction += aim.y
	if Input.is_action_pressed("down"): 
		direction -= aim.y
	
	direction = direction.normalized()

#MAX SPEED
	var target = direction * FLY_SPEED 
	velocity = velocity.linear_interpolate(target, FLY_ACCEL * delta )
#MOVEMENT
	move_and_slide(velocity * shift)

#ROTATE WITH MOUSE

func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
		var change = -event.relative.y * mouse_sensitivity
		if change + cam_angle < 90 and change + cam_angle > - 90:
			$"TSBM Drone".rotate_z(deg2rad(change))
			cam_angle += change

The one problem I have now is that it just doesn't go forward in the direction it's facing unless it's looking toward the horizon.

I've formatted your post, assuming that the all caps parts were supposed to be comments.

you can add ~~~ on a line before and after the code block to format it as a code block in your post.

~~~
    code line 1
    code line 2
    code line 3
    etc
~~~

@Megalomaniak said: I've formatted your post, assuming that the all caps parts were supposed to be comments.

you can add ~~~ on a line before and after the code block to format it as a code block in your post.

~~~
    code line 1
    code line 2
    code line 3
    etc
~~~

Thank you, yes they were comments, and the formatting made them look huge in the preview.

I have to somehow put the condition if z axis rotation is a certain angle, then a certain movement must happen. How do I tell it to check the angle of the Z axis of the KinematicBody?

This is where I'll need to add specifications in the code:

    if Input.is_action_pressed("forward") #&&  z axis rotation is < 360 & > 270 <- How do I tell it this? : 
        direction += aim.x - aim.y
    if Input.is_action_pressed("forward") #&&  z axis rotation is > 0 & < 90 <-- How do I tell it this? : 
        direction += aim.x+ aim.y
    if Input.is_action_pressed("forward") #&&  z axis rotation is = 90 <-- How do I tell it this? : 
        direction += aim.y
    if Input.is_action_pressed("forward") #&&  z axis rotation is = 270  <-- How do I tell it this? : 
        direction += - aim.y
    if Input.is_action_pressed("forward") #&&  z axis rotation is = 0 <-- How do I tell it this? : 
        direction += aim.x

EDIT: I finally got something that seems to be working and no longer gives me errors. Example:

	if Input.is_action_pressed("forward") && $"Mesh Name".rotation_degrees.z > 0 && $"Mesh Name".rotation_degrees.z < 90 
        direction += aim.x+ aim.y

EDIT2: It works for looking up, but I'm not sure why the angles for looking down don't work. I tried < 360 and > 270 for the situation of the drone looking down and using W, but nothing's happening.

EDIT3: Ok, I figured out what Godot's doing here. Now it works for downward too.

3 years later