首页 > 解决方案 > 转换为 3D 后,速度反射不再起作用

问题描述

我一直在制作基于勇者斗恶龙英雄火箭史莱姆的坦克战斗系统的游戏。我需要了解的第一件事是它的移动系统,玩家可以在其中伸展并朝一个方向移动。当他们撞到墙时,他们应该真实地反射它(例如:对角撞击它会对角反射它们)

我的游戏大部分是 2D 的,但在 2D 空间中发现伪 3D 碰撞以及其他问题使得它几乎不可能继续,所以我将其移至完全 3D。

一切都很顺利,直到我尝试从墙上弹起。如果我的球员在上坡时撞到了墙,他就会倒下,反之亦然。但是,如果我的球员向左撞墙,它会在应该将他反射到右侧时尝试将他“反射”到左侧(反之亦然,在向右移动时击中它)。

“速度”在我的游戏中的工作方式是,当拉伸名为 pVelocity 的 Vector2 时,会根据它们拉伸的程度而上升。当他们放手时,会根据 currentPosition + pVelocity 创建一个“endPos”。然后玩家将通过 Vector3.MoveTowards 以恒定速度移动到该 endPos。撞墙时,我这样做:

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

在 wallBounce 中,我这样做:

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 内部的某个地方,当我创建新的 endPos 时,但我不确定如何解决它。

标签: c#unity3dvectorphysics

解决方案


似乎问题是我的球员在向左或向右击球时没有看向运动方向。在我调用 MoveTowards 之前将其添加到部分中。

transform.LookAt(endPos);

推荐阅读