35 lines
787 B
C#
35 lines
787 B
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class RigidbodySpeedLimiter : MonoBehaviour
|
|
{
|
|
public float maxSpeed = 5f;
|
|
public float minSpeed = 2f;
|
|
|
|
private Rigidbody _rigidbody;
|
|
|
|
private void Start()
|
|
{
|
|
_rigidbody = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if (_rigidbody.IsSleeping()) return;
|
|
|
|
float velocity = _rigidbody.velocity.magnitude;
|
|
|
|
Debug.Log(velocity);
|
|
|
|
if (velocity < minSpeed)
|
|
{
|
|
_rigidbody.velocity = _rigidbody.velocity.normalized * minSpeed;
|
|
}
|
|
else if (velocity > maxSpeed)
|
|
{
|
|
_rigidbody.velocity = Vector3.ClampMagnitude(_rigidbody.velocity, maxSpeed);
|
|
}
|
|
}
|
|
} |