How to limit rotations? - 3D Orbit Camera

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Defaultsound
:warning: Old Version Published before Godot 3 was released.

Been trying to create a basic orbit camera. So far I’ve managed to get the mouse motion event to turn a camera Gimbal node. Works fine for Horizontal rotations.

However, I can’t seem to figure out how I can limit the Vertical Rotations. Ideally I want to limit the rotations between 1 and 89, as not to rotate over the top of the orbit pivot point.

extends Spatial


func _ready():
	set_process_input(true)

func _input(ev):
	if (ev.is_class("InputEventMouseMotion")):
		if (ev.button_mask&(BUTTON_MASK_LEFT)):
			
			var speed = ev.get_relative()
			
			rotate_y(clamp(speed.x,-0.1,0.1))

			get_node("InnerGimbal").rotate_x(clamp(speed.y,-0.1,0.1))

So I’ve tried clamping the rotation of the InnerGimbal, however when rotate_x is called it just ignores the clamp. So I’m not sure what to do.

if im not mistaken you need to clamp the final result, that way you wont be able to go above or below the limits, instead of clamping the speed of the rotation, thus making it possible(yet may take more time) to exceed the limitations.

rustyStriker | 2017-11-13 15:20

Yea I was thinking along those lines, however I can’t see how I can do that, where should I clamp the values between 1 and 89?

Defaultsound | 2017-11-13 15:32

instead of

rotate_y(clamp(speed.x,-0.1,0.1))

Try:

var rotation = get_rotation
rotation.y = clamp(rotation.y + speed.x, Min, Max)
set_rotation(rotation)

assuming they are all the correct functions and you are using Godot 2.1 branch…

rustyStriker | 2017-11-13 17:30