c# - 在 Unity 中增加速度时球会晃动

标签 c# unity-game-engine

我是初学者。我正在Unity中制作一个球类游戏,其中球必须避免与障碍物碰撞。在游戏中,我每 3 秒就会增加球速。一切正常,但在比赛进行到一半时,我注意到球开始晃动,速度降低,并且摄像机无法捕捉到球。我将物理 Material 附加到所有游戏对象上,并将所有摩擦力设为零,但球仍然在晃动。

这是我的游戏和问题的链接。看看: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);
}


}

最佳答案

正如 BugFinder 指出的那样,你实际上并没有用物理来移动球,而只是用 Transform.Translate 传送球,这可能会影响摇晃问题,另一个可能的问题是你的球速度由于与道路的碰撞,可能会有所不同,您是否尝试过将道路RigidBody设置为Kinematic?所以不会影响球的速度

关于c# - 在 Unity 中增加速度时球会晃动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67338429/

相关文章:

c# - 对于每个分辨率,UI 对象都位于相同的位置

c# - 如何使用双显示器应用程序解决这个焦点问题?

c# - SQL Server 安全通信

c# - 在 IIS 中运行时找不到 .dll.config 文件

unity-game-engine - 相机偏移|探戈计划

c# - 如何为推送通知解码 Unity 的 iOS 设备 token ?

c# - C# 中是否有任何方法或方式可以在 LIFO 的基础上添加 List<T> 中的项目?

c# - WPF RadioButton 命令绑定(bind)

c# - System.IO.DriveInfo 返回错误的磁盘空间值

unity-game-engine - 如何获取一些Unity内置资源的路径和文件大小?