[英]How can i stop cube from rolling
我有一個亞軍游戲,多維數據集是我的玩家,問題是我無法阻止多維數據集滾動。 地面很滑(摩擦= 0),但仍在滾動。 當我凍結y軸的旋轉時,它似乎滯后了,因此也不起作用。 請幫我。 有我的動作代碼
我更改了質量和阻力的值,但沒有幫助。
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public float acceleration;
public PlayerMovement movement;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
沒有錯誤消息。
您可以嘗試將Time.deltaTime替換為Time.fixedDeltaTime,因為它位於FixedUpdate中。
好吧,在嘗試為您提供幫助后,我編寫了一個有關您要實現的目標的小型演示,以便您可以使用它來嘗試找到以前出錯的地方以及我將如何去做您要去做的事情。
多維數據集控件
using UnityEngine;
public class CubeControl : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public float acceleration = 1;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
//Using the in-built methods uses the keys you were but also the arrow keys
float inputX = Input.GetAxis("Horizontal");
//Check there is input
if (Mathf.Abs(inputX) > float.Epsilon)
{
//set which force direction to use by comparing the inputX value
float force = inputX > 0 ? sidewaysForce : -sidewaysForce;
rb.AddForce(force * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}
那里有幾處更改,但已注釋以解釋。
相機追蹤
using UnityEngine;
public class CameraTracking : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private GameObject _cube;
#pragma warning restore 0649
private Vector3 offset;
void Awake()
{
offset = _cube.transform.position + transform.position;
}
void LateUpdate() {
transform.position = _cube.transform.position + offset;
}
}
它使用多維數據集與開始按下播放之前設置的“相機”之間的初始偏移。
提示 我建議不要使用此功能,而應使照相機成為多維數據集的子級,因為這是您計算每個幀的必要步驟!
路徑生成器
using UnityEngine;
using System.Collections;
public class PathGenerator : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private GameObject _path1;
[SerializeField] private GameObject _path2;
[SerializeField] private GameObject _cube;
#pragma warning restore 0649
private float _cubeRepositionZDistance;
private float _pathPositionX;
private float _pathPositionY;
//Distance center of path should be behind the cube;
private float _resetDistanceFromCube = 25f;
private void Awake()
{
// You could hard code this but this way you can change the length of each path segment and it will be updated here automatically.
_cubeRepositionZDistance += _path1.transform.localScale.z / 2;
_cubeRepositionZDistance += _path2.transform.localScale.z / 2;
_pathPositionX = _path1.transform.position.x;
_pathPositionY = _path1.transform.position.y;
// Position path2 relative to path1.transform.position
_path2.transform.position = new Vector3(_pathPositionX, _pathPositionY, _path1.transform.position.z + _cubeRepositionZDistance);
StartCoroutine(PathRepositioner());
}
private IEnumerator PathRepositioner()
{
//Can change bool to something like !GameOver
while (true)
{
if (_path1.transform.position.z < _cube.transform.position.z - _resetDistanceFromCube)
{
_path1.transform.position = new Vector3(_pathPositionX, _pathPositionY, _path2.transform.position.z + _cubeRepositionZDistance);
}
if (_path2.transform.position.z < _cube.transform.position.z - _resetDistanceFromCube)
{
_path2.transform.position = new Vector3(_pathPositionX, _pathPositionY, _path1.transform.position.z + _cubeRepositionZDistance);
}
yield return null;
}
}
}
這樣,您將重復使用相同的2個路徑方案,而不是一直都在創建克隆,可以根據需要將其更改為使用3個或更多。
場景設定
放置多維數據集,然后將path1段放置在多維數據集下方。
將所有必需的GameObjects分配給檢查器中的腳本。
按下播放!
旁注:建議像這樣的無盡奔跑者,而不是立方體(球員)移動路徑,但是由於您是新手,所以我建議您在有機會的情況下仔細閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.