(Unity)旋回できる角度を制限する(Clampの活用)

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankMovement3_AngleLock : MonoBehaviour
{
    public float turnSpeed;
    private Rigidbody rb;
    private float turnInputValue;

    // ★角度制限
    private float currentAngleY;

    void Start()
    {
        rb = GetComponen<rigidbody>();
    }

    void Update()
    {
        var forSpeed = Vector3.Dot(rb.velocity, transform.forward).ToString("0");

        if (Input.GetKey(KeyCode.LeftShift))
        {
            rb.velocity += transform.forward * 0.3f;
            rb.velocity = Vector3.ClampMagnitude(rb.velocity, 30);
        }

        if (Input.GetKey(KeyCode.RightShift))
        {
            rb.velocity -= transform.forward * 0.2f;
        }

        if (Input.GetKeyDown(KeyCode.Z))
        {
            rb.velocity = Vector3.zero;
        }

        TankTurn();

        // 角度制限
        // 現在の角度の取得
        currentAngleY = transform.localEulerAngles.y;
        
        // 角度制限
        // 角度が-180〜180の範囲内になるように補正
        if (currentAngleY > 180)
        {
            currentAngleY = currentAngleY - 360;
        }

        print(currentAngleY);

        // 角度制限
        // Clam関数を使って、-45度から45度の範囲内に制限
        currentAngleY = Mathf.Clamp(currentAngleY, -45, 45);
        transform.localEulerAngles = new Vector3(0, currentAngleY, 0);
    }

    void TankTurn()
    {
        turnInputValue = Input.GetAxis("Horizontal");

        transform.Rotate(new Vector3(0, turnInputValue, 0));
    }
}

(実行結果)