Windows11 Unity2021.3.11f.1  
TPSのゲームでエルデンリングみたいなゲームを作っていて、RigidBody.Addforceでプレイヤーを移動させています。カメラの向きに対してWASDで移動し、RigidbodyのVelocityのXとZのsqrMagnitudeで速度の制限をします。

ですがRigidbodyのVelocityが5,6フレーム毎に0になり、思っているような動作にならないです。  
Velocityの値で走るのと歩くアニメーションを切り替えているのでアニメーションがカクカクします。  
また、移動速度が想定道理にいかないです。

ネットでいろいろ解決策を調べましたが、求めている情報がなく解決しませんでした。  
また、2020.3.1f1のバージョンで作っていた同じゲームでは、まったく同じプログラムでRigidbodyの値も同じにしてもこの問題は起こりませんでした。  
変わったこととしては、URPのプロジェクトでUnityChanSpringBoneとUniversalToonShaderforURPっていうPackageを使って(?)ます。  
あとisSleeping()は常にFalseでした。

↓多分必要そうなの

```

    void Update()
        {
            var horizontal = Input.GetAxis("Horizontal");
            var vertical = Input.GetAxis("Vertical");
            var cameraRotation = Quaternion.AngleAxis(cameraTransform.eulerAngles.y, Vector3.up); //Cameraの角度
            var playerVector = cameraRotation * new Vector3(horizontal, 0, vertical).normalized;//進む方向
            var velocity = new Vector2(rb.velocity.x, rb.velocity.z).sqrMagnitude; //Playerの速度
            var speed = runSpeed;
            var speedLimit = runSpeedLimit;
            animator.SetFloat("Velocity", velocity);
            Debug.Log(velocity);
    
            //↓移動させてる
            if (velocity < speedLimit)
            {
                rb.AddForce(playerVector * speed, ForceMode.Accelaration); 
            }
    
```

↓全文

```

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    
    public class PlayerController : MonoBehaviour
    {
        int health = 1000, stamina = 1000, mana = 1000;
        Transform playerTransform, cameraTransform;
        Rigidbody rb;
        Animator animator;
        Quaternion targetRotation;
        float runSpeed = 100, sprintSpeed = 200, runSpeedLimit = 15, sprintSpeedLimit = 30;
        bool moveable = true, staminable = true;
        enum playerState
        {
            LOCOMOTION,
            ATTACK,
        }
        playerState state = playerState.LOCOMOTION;
    
        void Awake()
        {
            Application.targetFrameRate = 60;
        }
        void Start()
        {
            playerTransform = gameObject.transform;
            cameraTransform = Camera.main.transform;
            rb = gameObject.GetComponent<Rigidbody>();
            animator = gameObject.GetComponent<Animator>();
        }
    
        void Update()
        {
            PlayerDead();
            Move();
            Attack();
        }
    
        void Move()
        {
            var horizontal = Input.GetAxis("Horizontal");
            var vertical = Input.GetAxis("Vertical");
            var cameraRotation = Quaternion.AngleAxis(cameraTransform.eulerAngles.y, Vector3.up); //Cameraの角度
            var playerVector = cameraRotation * new Vector3(horizontal, 0, vertical).normalized;//進む方向
            var velocity = new Vector2(rb.velocity.x, rb.velocity.z).sqrMagnitude; //Playerの速度
            var speed = runSpeed;
            var speedLimit = runSpeedLimit;
            animator.SetFloat("Velocity", velocity);
            Debug.Log(velocity);
            Debug.Log(rb.IsSleeping());
    
            //回転
            if (playerVector.sqrMagnitude >= 0.25f)
            {
                targetRotation = Quaternion.LookRotation(playerVector, Vector3.up);
            }
            transform.rotation = Quaternion.RotateTowards(playerTransform.rotation, targetRotation, 25.0f);
    
            //スタミナ
            if (stamina <= 1)
            {
                staminable = false;
            }
            else if (stamina >= 100)
            {
                staminable = true;
            }
    
            //移動速度
            if (Input.GetKey(KeyCode.LeftShift) && staminable)
            {
                speed = sprintSpeed;
                speedLimit = sprintSpeedLimit;
                stamina--;
            }
            else
            {
                if (stamina < 1000)
                {
                    stamina++;
                }
            }
    
            //移動
            if (velocity < speedLimit)
            {
                rb.AddForce(playerVector * speed, ForceMode.Acceleration); ;
            }
        }
    
        void Attack()
        {
    
        }
    
        void PlayerDead()
        {
    
        }
    }
    
    
```

↓Console  
3.402671  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402681  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

0  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

14.47523  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402697  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.40266  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402671  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

0  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

14.47524  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

0  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

14.47524  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

False  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:53)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)

3.402676  
UnityEngine.Debug:Log (object)  
PlayerController:Move () (at Assets/Scripts/PlayerController.cs:52)  
PlayerController:Update () (at Assets/Scripts/PlayerController.cs:38)
CC BY-SA 4.0
2022-10-04T13:27:47.193
91449
2022-10-04T13:27:47.193
54699
question
c# unity3d
RigidBodyのSpeedが数フレームごとに0になる