c# - 转换为 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 时,但我不确定如何解决它。
解决方案
似乎问题是我的球员在向左或向右击球时没有看向运动方向。在我调用 MoveTowards 之前将其添加到部分中。
transform.LookAt(endPos);
推荐阅读
- javascript - animate.css 在 Chrome 最新版本(73 以上版本)中不起作用
- python - Numpy/Pytorch dtype 转换/兼容性
- c# - 什么使控制台应用程序项目
- spring-boot - spring kafka - 无法产生消息和事务回滚不起作用 - org.apache.kafka.common.errors.ProducerFencedException
- javascript - 使用 javascript 中的过滤器转换简单逻辑
- java - kafka 消费者组线程的线程顺序在每次启动时都不同
- python - 如何在使用 Papermill 的 Python 客户端执行 Jupyter 笔记本时指定内核?
- android - 构建失败的颤振
- java - 未输入 ViewHolder 中的 OnClickListener
- font-awesome - 在甜蜜警报对话框上显示字体真棒微调器