【问题标题】:Why the Rigidbody player controller is not working ? I can't move the player around为什么刚体播放器控制器不工作?我无法移动播放器
【发布时间】:2020-04-16 21:27:59
【问题描述】:

这个脚本附加到我的具有刚体组件的播放器上。 刚体使用重力和运动学设置为ture。

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

public class RigidbodyPlayercontroller : MonoBehaviour
{
    Rigidbody rb;
    public float speed;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float mH = Input.GetAxis("Horizontal");
        float mV = Input.GetAxis("Vertical");
        rb.velocity = new Vector3(mH * speed, rb.velocity.y, mV * speed);
    }
}

但是玩家没有移动它什么都不做。我尝试了速度值 1 和 100。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    设置运动刚体的velocity 不会产生任何影响。如文档所述,使用MovePosition 更改运动学Rigidbody 的位置。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController: MonoBehaviour
    {
       public float speed;
    
       private float vertical;
       private float horizontal;
    
       private Rigidbody rb;
    
       void Start()
       {
          rb = GetComponent < Rigidbody > ();
       }
    
       void Update()
       {
          vertical = Input.GetAxis("Vertical") * speed;
          horizontal = Input.GetAxis("Horizontal") * speed;
       }
    
       void FixedUpdate()
       {
          rb.MovePosition(
             transform.position +
             transform.right * horizontal * Time.fixedDeltaTime +
             transform.forward * vertical * Time.fixedDeltaTime
          );
       }
    }
    

    【讨论】:

      【解决方案2】:

      这是 2d 还是 3d 游戏?我对 3d 游戏不太了解,但那段代码似乎就像一个 2d 游戏控制器,如果我错了,请告诉我,我可以帮助你

      【讨论】:

      • 这是一个 3D 游戏。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多