【问题标题】:Ball shaking upon increasing its speed in Unity球在 Unity 中增加速度时抖动
【发布时间】:2021-05-01 03:01:30
【问题描述】:

我是初学者。我正在 Unity 中制作球类游戏,其中球必须避免与障碍物发生碰撞。在游戏中,我每 3 秒增加一次球速。一切正常,但在比赛进行到一半时,我注意到球开始晃动,速度降低,并且相机无法接球。我将物理材质附加到所有游戏对象,并使所有摩擦为零,但球仍在摇晃。

这是我的游戏和问题的链接。看看:https://youtu.be/TR4M5whweTk

这是附在球上的脚本:

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

public class PlayerController : MonoBehaviour
{
public Text gameOverText;
public Text scoreText;
public bool isGameOver;
public float speeder;

public float Score;
Touch touch;
public float speedmodifier ;

public float speed = 5;
// Start is called before the first frame update

private void Awake()
    {
    isGameOver = false;
    Score = 0;
    if(PlayerPrefs.GetFloat("HighScore") == 0)
    {
        PlayerPrefs.SetFloat("HighScore", 0);
    }
    PlayerPrefs.SetFloat("Score", Score);
}
void Start()
{
    speeder = 4f;
    gameOverText.enabled = false;
    speedmodifier = 0.01f;
   // GetComponent<Rigidbody>().velocity = new Vector3(0,0,speed);

}

// Update is called once per frame
void Update()
{   if(speeder >= 0)
    {
        speeder -= Time.deltaTime;
    }
    if (speeder<= 0 && speed < 50)
    {
        speed++;
        speeder = 4f;
   
    }
    Debug.Log(speed); 
    if (isGameOver == false)
    {
        Score++;
    }
    

    scoreText.text = "Score : " + Score ;
    
    if (Input.touchCount >0 && transform.position.x >= -3.5f &&  transform.position.x <= 3.5f)
    {
        touch = Input.GetTouch(0);
        transform.Translate(touch.deltaPosition.x * speedmodifier,0,0);
    }
    else if(transform.position.x > 3.5f)
    {
        transform.position = new Vector3(3.49f,transform.position.y,transform.position.z);
    }
    else if(transform.position.x < -3.5f)
    {
       transform.position = new Vector3(-3.49f,transform.position.y,transform.position.z) ;
    }
    

     if (Input.GetKey(KeyCode.RightArrow) && transform.position.x < 3.5f)
    {
        transform.Translate(Vector3.right*speed* Time.deltaTime);
    }


     if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x >-3.5f)
    {
        transform.Translate(Vector3.left*speed* Time.deltaTime);
    }

     
   transform.Translate(Vector3.forward * speed * Time.deltaTime);
}

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "enemy")
    {
        isGameOver = true;
        StartCoroutine("Wait");
        GetComponent<MeshRenderer>().enabled = false;
        gameObject.GetComponentInChildren<TrailRenderer>().enabled = false;
        gameOverText.enabled = true;

        if(PlayerPrefs.GetFloat("HighScore") < Score)
        {
            PlayerPrefs.SetFloat("HighScore", Score);
            PlayerPrefs.SetFloat("Score", Score);
        }
        else
        {
            PlayerPrefs.SetFloat("Score", Score);
        }

    }
}

IEnumerator Wait()
{
    Debug.Log(" My HighScore is : " + PlayerPrefs.GetFloat("HighScore"));
    Debug.Log(" Score is : " + PlayerPrefs.GetFloat("Score"));
    yield return new WaitForSeconds(3f);
    SceneManager.LoadScene(1);
}


}

【问题讨论】:

  • 你看起来是在不移动的点之间用物理传送它
  • 像所有其他答案一样:不要使用物理,只需使用Transform.Translate(Vector3.forward * speed * Time.deltaTime); 不需要物理,因为它会计算重力和碰撞等,但如果你只是前进,可能以及修复错误并提高FPS。如果你想让游戏产生水滴和东西,只需添加你自己的重力。
  • 当我从球上移开重力时,球不再摇晃了。

标签: c# unity3d


【解决方案1】:

您的问题

我只是想确保我首先正确理解了你的问题,我相信你在寻求一种方法来防止球在比赛中晃动。 (让我知道这是否不正确,如果是,我深表歉意)。从我收集到的信息来看,这似乎不是基于代码的问题

碰撞检测

在统一中,刚体组件有一个名为“碰撞检测”的字段,默认情况下设置为离散。这意味着团结会不时地寻找可能的对撞机的行进方向。在尝试节省计算机资源时,这是一种不错的方法,但是在持续接触或高速时,最好使用“连续”模式。这将检查每个物理时间步发生的每个物理更新的碰撞。

碰撞器

统一的碰撞器有点挑剔。最臭名昭著的是网格对撞机,因为它们必须进行更多的处理/计算。对地面使用盒式对撞机可能会得到更准确的物理结果。

其他解决方案

查看您在 YouTube 上发布的游戏视频时,似乎使用物理可能无法正常工作。由于您只在全球范围内向左、向右和向前移动,我建议您实现一个使用对象的变换组件而不是物理的移动脚本。从长远来看,这将减少资源密集型,并且会省去更多的调试麻烦。

【讨论】:

  • 你们已经猜到了,这种游戏根本不是用物理来完成的。你基本上只是“移动精灵”。
【解决方案2】:

正如 BugFinder 指出的那样,您实际上并没有通过物理方式移动球,而只是使用 Transform.Translate 传送球,这可能会影响摇晃问题,另一个可能的问题是您的球速可能会因碰撞而发生变化有了这条路,你有没有试过把路RigidBody变成Kinematic?所以不会影响球的速度

【讨论】:

  • 你们已经猜到了,这种游戏根本不是用物理来完成的。你基本上只是“移动精灵”。
【解决方案3】:

好的,你不应该为此使用统一物理。

这是一个“光栅”游戏(这很有趣)。

  1. 删除所有物理一切。完全移除刚体、碰撞器等

  2. 每次只需使用计算移动球和/或风景。

(显然,它基本上只是帧时间 * 速度。)


(请注意,您可以严格使用 触发器 - 如果您愿意 - 只需知道“球”是否靠近“棒”。但您可以用 1 行代码做到这一点,真的不需要对撞机/等)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多