C# 转换为3D后,速度反射不再起作用

C# 转换为3D后,速度反射不再起作用,c#,unity3d,vector,physics,C#,Unity3d,Vector,Physics,一段时间以来,我一直在制作一款基于龙之旅英雄火箭黏液坦克战斗系统的游戏。首先我需要放下的是它的运动系统,在这个系统中,玩家可以伸展身体,朝着一个方向抛掷自己。当他们撞到墙上时,他们应该真实地反映出来(例如:斜向撞墙将使他们斜向反射) 我的游戏大部分都是2D的,但是在2D空间中找出伪3d碰撞以及其他问题使得它几乎不可能继续,所以我把它移到了全3d 在我试着从墙上弹下来之前,一切都很顺利。如果我的球员撞上了一堵墙,他就会倒下,反之亦然。然而,如果我的玩家向左撞墙,它会尝试在应该向右反射的时候向左“反

一段时间以来,我一直在制作一款基于龙之旅英雄火箭黏液坦克战斗系统的游戏。首先我需要放下的是它的运动系统,在这个系统中,玩家可以伸展身体,朝着一个方向抛掷自己。当他们撞到墙上时,他们应该真实地反映出来(例如:斜向撞墙将使他们斜向反射)

我的游戏大部分都是2D的,但是在2D空间中找出伪3d碰撞以及其他问题使得它几乎不可能继续,所以我把它移到了全3d

在我试着从墙上弹下来之前,一切都很顺利。如果我的球员撞上了一堵墙,他就会倒下,反之亦然。然而,如果我的玩家向左撞墙,它会尝试在应该向右反射的时候向左“反射”他(反之亦然,在向右撞墙的时候)

在我的游戏中,“速度”的工作方式是,当拉伸一个称为pVelocity的矢量2时,它会根据拉伸的程度上升。当他们放手时,将根据currentPosition+pVelocity创建一个“endPos”。然后,玩家将通过矢量3移动。以恒定速度向该端点移动。当碰壁时,我会这样做:

if (hit && foundHit.transform!=transform && curState != state.wallHit && foundHit.transform.tag!="Object")
{
    //we've hit a wall while blasting

    //now we make the player 'squish' up against the wall and bounce off!

    Vector3 reflectedVelocity = Vector3.Reflect(new Vector3(pVelocity.x,0,pVelocity.y), foundHit.normal);

    //playerAnimator.SetFloat("VelocityX", reflectedVelocity.x);
    //playerAnimator.SetFloat("VelocityY", reflectedVelocity.y);

    curState = state.wallHit;
    playerSound.Play();
    transform.position = foundHit.point;

    playerAnimator.Play("Squish");
    StartCoroutine(wallBounce(reflectedVelocity));
}
IEnumerator wallBounce(Vector3 hitVelocity)
{
    playerAnimator.enabled = true;

    yield return new WaitForSeconds(.5f);
    playerAnimator.Play("Walk blend tree");
    pVelocity = new Vector2(hitVelocity.x,hitVelocity.z);

    endPos = transform.position + (-transform.forward * pVelocity.magnitude);
    curState = state.blasting;

    Vector3 diff = endPos - transform.position;
    diff.Normalize();

    float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, rot_z - 90, 0f);
}
在wallBounce,我这样做:

if (hit && foundHit.transform!=transform && curState != state.wallHit && foundHit.transform.tag!="Object")
{
    //we've hit a wall while blasting

    //now we make the player 'squish' up against the wall and bounce off!

    Vector3 reflectedVelocity = Vector3.Reflect(new Vector3(pVelocity.x,0,pVelocity.y), foundHit.normal);

    //playerAnimator.SetFloat("VelocityX", reflectedVelocity.x);
    //playerAnimator.SetFloat("VelocityY", reflectedVelocity.y);

    curState = state.wallHit;
    playerSound.Play();
    transform.position = foundHit.point;

    playerAnimator.Play("Squish");
    StartCoroutine(wallBounce(reflectedVelocity));
}
IEnumerator wallBounce(Vector3 hitVelocity)
{
    playerAnimator.enabled = true;

    yield return new WaitForSeconds(.5f);
    playerAnimator.Play("Walk blend tree");
    pVelocity = new Vector2(hitVelocity.x,hitVelocity.z);

    endPos = transform.position + (-transform.forward * pVelocity.magnitude);
    curState = state.blasting;

    Vector3 diff = endPos - transform.position;
    diff.Normalize();

    float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, rot_z - 90, 0f);
}

当吊起时,球员总是朝着他们要去的方向,所以我假设当我创建新的endPos时,问题在wallBounce内部的某个地方,但我不确定如何修复它。

问题似乎是我的球员在左击或右击时没有注意移动方向。在调用MoveToward之前,将此添加到零件中

transform.LookAt(endPos);