(Unity)Clampを使って速度を制限する。

(スクリプト)

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

public class TankMovement2_Velocity : MonoBehaviour
{
    public float turnSpeed;
    private Rigidbody rb;
    private float turnInputValue;
    public Text velocityLabel;
    
    void Start()
    {
        rb = GetComponen<Rigidbody>();  
    }

    void Update()
    {
        // 改良(0未満の小数は表示しない)
        var forSpeed = Vector3.Dot(rb.velocity, transform.forward).ToString("0");
        velocityLabel.text = "速度:" + forSpeed;

        if (Input.GetKey(KeyCode.LeftShift))
        {
            rb.velocity += transform.forward * 0.3f;

            // 改良(Clampを使って速度を制限する)
            rb.velocity = Vector3.ClampMagnitude(rb.velocity, 30);
        }

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

        TankTurn();

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

    void TankTurn()
    {
        turnInputValue = Input.GetAxis("Horizontal");
        float turn = turnInputValue * turnSpeed * Time.deltaTime;
        Quaternion turnRotation = Quaternion.Euler(0, turn, 0);
        rb.MoveRotation(rb.rotation * turnRotation);
    }
}

(実行結果)