简体   繁体   English

如何阻止多维数据集滚动

[英]How can i stop cube from rolling

i got a runner game and cube is my player, the problem is i can't stop cube from rolling. 我有一个亚军游戏,多维数据集是我的玩家,问题是我无法阻止多维数据集滚动。 The ground is slippery(friction = 0) but it is still rolling. 地面很滑(摩擦= 0),但仍在滚动。 When i freeze rotation of y axis it seems like lagging so it doesn't work either. 当我冻结y轴的旋转时,它似乎滞后了,因此也不起作用。 Please help me. 请帮我。 There is my movement code 有我的动作代码

I changed values of mass and drag but it didn't help. 我更改了质量和阻力的值,但没有帮助。

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);
    }

There are no error messages. 没有错误消息。

您可以尝试将Time.deltaTime替换为Time.fixedDeltaTime,因为它位于FixedUpdate中。

Okay so after trying to help you in the comments I have written a small demo of what you are trying to achieve so you can use it to try and find where you went wrong before and how I would go about doing what you are trying to do. 好吧,在尝试为您提供帮助后,我编写了一个有关您要实现的目标的小型演示,以便您可以使用它来尝试找到以前出错的地方以及我将如何去做您要去做的事情。

Cube Controls 多维数据集控件

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);
        }

    }
}

There are a couple of changes there but they are commented to explain. 那里有几处更改,但已注释以解释。

Camera Tracking 相机追踪

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;
    }
}

It uses the initial offset between the cube and Camera that is setup before starting the pressing play. 它使用多维数据集与开始按下播放之前设置的“相机”之间的初始偏移。

Tip I recommend not using this but making the camera a child of the cube as this is something that you are calcualting each frame which you don't need too! 提示 我建议不要使用此功能,而应使照相机成为多维数据集的子级,因为这是您计算每个帧的必要步骤!

Path Generator 路径生成器

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;
        }
    }

}

Doing it this way you are reusing the same 2 path segements and not creating clones all the time, you can change it to use 3 or more if needed. 这样,您将重复使用相同的2个路径方案,而不是一直都在创建克隆,可以根据需要将其更改为使用3个或更多。

Scene Setup 场景设定

Position the cube and then position the path1 segement under the Cube. 放置多维数据集,然后将path1段放置在多维数据集下方。

Assign all required GameObjects to the scripts in the inspector. 将所有必需的GameObjects分配给检查器中的脚本。

Press Play! 按下播放!

Sidenote: It is recommended to move the path segements not the Cube(Player) when doing an endless runner like this but as you are new to this I suggest reading up on that when you get a chance. 旁注:建议像这样的无尽奔跑者,而不是立方体(球员)移动路径,但是由于您是新手,所以我建议您在有机会的情况下仔细阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM