我有一個奇怪的問題,似乎無法解決它
我正在創建我的第一個統一游戲,我在創建一個運動系統后進行了一些測驗,每當我觸摸另一個物體(它是否有剛體都沒有關系)時,我的玩家突然開始自己移動。
我有一個視頻顯示究竟發生了什么:https : //youtu.be/WGrJ0KNYSr4
我已經嘗試了一些不同的東西,我確定它必須對物理引擎做一些事情,因為它只會在玩家不是運動學的時候發生,所以我嘗試在專案設定中增加物理求解器迭代,但錯誤仍然存??在發生了這樣的事情,所以我在互聯網上尋找答案,但我發現的唯一一件事是洗掉 Time.deltaTime,但它仍然無法正常作業。我發現當玩家快速移動時,它似乎發生得更少。
如果有人能幫我解決這個問題,我真的很感激,因為這是我的第一個真正的游戲,我正在為 itch.io 上發生的 Seajam 創建它。
這是我的 playercontroller 腳本的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float playerSpeed = 3f;
private Rigidbody playerRb;
public GameObject cameraRotation;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
}
float verticalSpeed;
float horizontalSpeed;
bool isOnGround = true;
// Update is called once per frame
void Update()
{
//get controlled forward and backward motion
verticalSpeed = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * playerSpeed * verticalSpeed * Time.deltaTime);
//get controlled sidewards motion
horizontalSpeed = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * playerSpeed * horizontalSpeed * Time.deltaTime);
//lock the rotation of the player on the z and x axis
transform.eulerAngles = new Vector3(0, cameraRotation.transform.eulerAngles.y, 0);
//when pressing space jump and prevent air jump
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
playerRb.AddForce(Vector3.up * 10, ForceMode.Impulse);
isOnGround = false;
}
}
//check if the player is on the ground
private void OnCollisionEnter(Collision collision)
{
isOnGround = true;
}
}
uj5u.com熱心網友回復:
盡量不要通過腳本鎖定播放器的旋轉,可能是由于萬向節鎖定導致的問題。而是轉到播放器的 RigidBody->Constraints,然后將其鎖定。你可以在這里閱讀更多關于它的資訊https://fr.wikipedia.org/wiki/Blocage_de_cardan
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351171.html
上一篇:在Unity中,為什么我可以通過按下按鈕將相機切換到WorldView,但不能在使用FaceManager的同時在開始或更新回圈中執行此操作?
