在Unity3D中无法精确定位刚体 [英] Can not get precise positioning of Rigidbody in Unity3D

查看:56
本文介绍了在Unity3D中无法精确定位刚体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个与标准略有不同的 3D 无尽跑酷.有 1 个单位宽的 5 条车道,如果它们向左或向右移动太远,玩家可能会从两侧掉下来.玩家不会向前移动、跳跃或滑动.它只有向左或向右移动.如果玩家被迎面而来且速度随时间增加的障碍物击中,玩家可能会向后弹起.

I am creating a 3D endless runner that's a little different than the norm. There are 5 Lanes that are 1 unit wide, and the player can fall off the sides if they move too far to the left or right. The Player does not move Forward, Jump or Slide. It only has left or right movement. The player can get bounced backward if hit by an oncoming obstacle that increases in speed over time.

关于玩家的左/右移动,我非常接近.但是我无法准确定位播放器.

In regards to the player's left/right movement, I got it pretty close. But I'm having trouble getting precise positioning of the player.

有时当玩家越过一条车道时,位置有时会偏离 0.1,并且随着时间的推移可能会开始堆积.即从 0 位置开始,向左移动,在 0.9 或 1.1 处停止.

Sometimes when the player moves over a lane the position will sometimes be off by 0.1, and may start to stack up over time. i.e. Starting in the 0 position, move left, it stops at 0.9 or 1.1.

这是目前的代码 -

{
public float speed;
public float mleft = -0.7f;
public float mright = 0.7f;

private Rigidbody rb;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void FixedUpdate()
{
    if (Input.GetKeyDown("a"))
    {
        rb.velocity = new Vector3(mleft, 0, 0) * speed;
        StartCoroutine(stopLaneCH());
    }

    Debug.Log(GetComponent<Transform>().position);

    if (Input.GetKeyDown("d"))
    {
        rb.velocity = new Vector3(mright, 0, 0) * speed;
        StartCoroutine(stopLaneCH());
    }
    

}

IEnumerator stopLaneCH ()
{
    yield return new WaitForSeconds(0.5f);
    GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
}

}

我不得不尝试在 speedmleft/mright 变量之间找到一个平衡点,以便让玩家移动 1 个单位.

I've had to try and find a balance between the speed and the mleft/mright variables that would move the player 1 unit.

我对 Unity 和编程还是很陌生,但非常感谢您对这个问题的任何帮助.

I am still fairly new to Unity and programming, but any help with this problem is greatly appreciated.

我什至可以用任何帮助解决这个问题的人来命名我的第三个孩子.

推荐答案

如果我的理解是正确的,不要改变速度,因为要让它精确移动 1 个单位几乎是不可能的.相反,为什么不将玩家移动一个单位确切地?使用协程,您可以使用 Vector3.Lerp 方法随着时间的推移从当前位置移动到所需位置.您想要的位置是 1 个单位,或者,您的每条车道与您当前位置之间的距离.

If I am understanding this correct, instead of changing the velocity, because to get it to move 1 unit exactly would be near impossible. Instead, why not move the player one unit exactly? Using a Coroutine you can use the Vector3.Lerp method to move from your current position to your desired position over time. Your desired position is 1 unit, or, the distance between each of your lanes from your current position.

void Update(){

    //For Moving Left
    if (Input.GetKeyDown(KeyCode.A))
            StartCoroutine(MoveToPosition(transform, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), 0.5f));
    
    //For Moving Right
    if (Input.GetKeyDown(KeyCode.D))
            StartCoroutine(MoveToPosition(transform, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), 0.5f));
}

public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
    {
        var currentPos = transform.position;
        var t = 0f;
        while (t < 1)
        {
            t += Time.deltaTime / timeToMove;
            transform.position = Vector3.Lerp(currentPos, position, t);
            yield return null;
        }
    }

我假设您在 X 轴上向左/向右移动,如果不是,只需将 - 1 更改为您正在移动的轴.您也可以根据自己的喜好更改 TimeToMove 浮动.

I presume you are moving left/right on the X axis, if not, simply change the - 1 to the axis that you are moving on. You can change the TimeToMove float to your liking as well.

希望这会有所帮助.如果您有任何问题,请随时提出!

Hope this helps. If you have any questions feel free to ask!

这篇关于在Unity3D中无法精确定位刚体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆