【问题标题】:Unity roll-a-ball - ball wont roll faster than 3.8... sometimesUnity roll-a-ball - 球的滚动速度不会超过 3.8... 有时
【发布时间】:2018-07-27 10:38:20
【问题描述】:

我一直致力于学习部分的滚球项目。我最近添加了一个“速度”文本字段,我注意到当我第一次开始播放时,如果按 W 或 S,那么速度不会超过 3.8(或低于 -3.8)。如果我只向左或向右移动一根头发,这将解决问题,我可以像往常一样使用 W 和 S 移动。如果我将球撞到墙上,也会发生同样的问题。如果我在编辑器中从 (.0000001, .5, 0) 而不是 (0, .5, 0) 开始球,也不会发生故障。以防万一我把代码搞砸了,我会把它贴在下面。

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

 public class PlayerController : MonoBehaviour {
     public float moveSpeed;
     public float accelSpeed;
     private int pickupCount;
     public Text countText;
     public Text winText;
     public Text speedGage;

     void Start(){
         pickupCount = 0;
         setCountText();
         winText.text = "";
         speedGage.text = "0";
     }
     void FixedUpdate(){
         updateMovement();

     }
     void updateMovement(){
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         Vector3 movement = new Vector3(moveHorizontal,0,moveVertical);
         rigidbody.AddForce(movement * moveSpeed * Time.deltaTime);
         speedGage.text = rigidbody.velocity.ToString();
     }
     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "PickUp"){
             other.gameObject.SetActive(false);
             pickupCount += 1;
             setCountText();
         }
     }
     void setCountText(){
         countText.text = "Count: " + pickupCount.ToString();
         if(pickupCount == 16){
             winText.text = "You Win!";
         }
     }
 }

【问题讨论】:

    标签: unity3d game-physics


    【解决方案1】:

    这个问题已经有一段时间没有发布答案了,所以如果你还没有找到解决方案,或者其他人可能会发现自己正在寻找答案......

    在rigidbody.AddForce 调用中,运动矢量乘以movespeed 和Time.deltaTime,得到一个非常小的值。试着把它改成这样:

    rigidbody.AddForce(movement * movespeed, Forcemode.Force);
    

    或者,如果您想忽略球的质量以突然改变方向,请使用 Forcemode.VelocityChange。

    时间已经计入了刚体.AddForce,只要球没有卡在障碍物上,结果就会由于对速度的影响而展开为以下物理步骤。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多