0 votes

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°?

in Engine by (199 points)
edited by

1 Answer

+1 vote
Best answer

In the _physics_process function after changing rotation.

rotation = clamp(rotation, deg2rad(-45), deg2rad(45))
by (3,894 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.