【问题标题】:Bouncing/reflecting my player off walls从墙上弹跳/反射我的播放器
【发布时间】:2018-08-03 12:07:54
【问题描述】:

我正在 DS 上使用勇者斗恶龙英雄火箭史莱姆重建一个运动系统。在该游戏中,玩家可以按住 A 按钮并将史莱姆主角向一个方向伸展。在 A 被拉长的时候放开它会使它们朝那个方向甩掉。

这是它的 gif 图像:

注意他是如何从对角线上反弹的。我的复制目前看起来像这样(忽略错误动画:P)

看起来不错!但是,如果我要斜着撞墙,我只会朝相反的方向向后移动,而不是反射它。

这是我如何处理墙壁反弹的代码。

IEnumerator wallBounce(Vector2 hitVelocity)
{
    playerAnimator.enabled = true;

    yield return new WaitForSeconds(.3f);
    playerAnimator.CrossFade("Walking " + curDirection, 0); // place holder animation
    pVelocity = Vector2.Scale(-pVelocity,new Vector2(0.25f,0.25f));
    endPos = transform.position + new Vector3(pVelocity.x, pVelocity.y);

    transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
    curState = state.blasting;

    transform.position = Vector3.MoveTowards(transform.position, endPos, 5f * Time.deltaTime);

}

我知道 Unity 中的 Vector3.Reflect 方法,但我不确定如何在我当前的代码中实现它。

【问题讨论】:

    标签: c# unity3d vector


    【解决方案1】:

    我好像做到了。

    我得到这样的反射向量:

    Vector3 reflectedVelocity = Vector3.Reflect(pVelocity*.5f, foundHit.normal);
    

    然后将其作为 hitVelocity 传递给 wallBounce。 StartCoroutine(wallBounce(reflectedVelocity));

    然后我改变了这一行:

    endPos = transform.position + new Vector3(pVelocity.x, pVelocity.y);
    

    进入

    endPos = transform.position + hitVelocity;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多