How do I limit my maximum turn in my code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By System_Error

Hi all. Just want a little help with my project.

I’ve got a turret that is fully capable of turning left and right. I’d like to impose a limit to how far to the left or right the turret may turn. The code below is my LazerGun.

extends KinematicBody2D

const LAZER = preload("res://player-side/EnergyShot.tscn")
const ROTATION_SPEED = 0.5

signal fire
var can_shoot = true
var rotation_Direction


func _ready():
	pass

func get_input():
	rotation_Direction = 0.0
	if Input.is_action_pressed('test_up'):
		rotation_Direction -= 1.0
	elif Input.is_action_pressed('test_down'):
		rotation_Direction += 1.0
	
	
	if Input.is_action_just_pressed('test_fire'):
		fire()


func fire():
	var lazershot = LAZER.instance()
	lazershot.start($LazerSpawn.global_position, rotation)
	get_parent().add_child(lazershot)


func _physics_process(delta):
	get_input()
	rotation += rotation_Direction * ROTATION_SPEED * delta


func _on_FireRate_timeout():
	can_shoot = true

I’d like to have a clamp() in there, but how do I limit the rotation to 45°/-45°?

:bust_in_silhouette: Reply From: SIsilicon

In the _physics_process function after changing rotation.

rotation = clamp(rotation, deg2rad(-45), deg2rad(45))

Thank you. I’d been wondering how exactly to do it.

System_Error | 2018-12-15 19:11