c# - 玩家旋转时子弹实例化角度错误
问题描述
我创建了一个具有 3 个不同实例化位置的弹丸子弹效果。当玩家面向右侧时,它可以完美运行。但是当面向左时,子弹实例化的角度会误入歧途。任何帮助表示赞赏。
玩家右转时的截图。
玩家左转时的截图。
播放器的代码。
private IEnumerator FireContinuously()
{
while (true)
{
GameObject laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10)));
laser02.GetComponent<Rigidbody2D>().velocity = laser02.transform.right * projectileSpeed * direction;
GameObject laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Euler(new Vector3(0, 0, 0)));
laser03.GetComponent<Rigidbody2D>().velocity = laser03.transform.right * projectileSpeed * direction;
GameObject laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345)));
laser04.GetComponent<Rigidbody2D>().velocity = laser04.transform.right * projectileSpeed * direction;
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
public void Flipsprite()
{
bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > 0;
if (playerhashorizontalspeed)
{
direction = Mathf.Sign(myRigidBody.velocity.x);
transform.localScale = new Vector3(direction, 1f);
}
}
解决方案
您正在使用硬编码的角度10
,345
无论您看向哪个方向。
然后使用 eglaser02.right
总是返回与Vector3
你否定它相同的结果。这会导致错误的方向。如果你比较图像,这是你得到的方向。
你更想要的是否定你旋转子弹的角度。
private IEnumerator FireContinuously()
{
while (true)
{
var laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10 * direction)));
laser02.GetComponent<Rigidbody2D>().velocity = laser02.transform.right * projectileSpeed * direction;
var laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Identity)
laser03.GetComponent<Rigidbody2D>().velocity = laser03.transform.right * projectileSpeed * direction;
var laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345 * direction)));
laser04.GetComponent<Rigidbody2D>().velocity = laser04.transform.right * projectileSpeed * direction;
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
还有一点提示:
如果您更愿意制作类型的预制件
[SerializeField] private RigidBody2D bullet01;
[SerializeField] private RigidBody2D bullet02;
[SerializeField] private RigidBody2D bullet03;
然后Instantiate
将直接返回相应的RigidBody2D
引用,您可以摆脱GetComponent
调用:
private IEnumerator FireContinuously()
{
while (true)
{
var laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10 * direction)));
laser02.velocity = laser02.transform.right * projectileSpeed * direction;
var laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Identity)
laser03.velocity = laser03.transform.right * projectileSpeed * direction;
var laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345 * direction)));
laser04.velocity = laser04.transform.right * projectileSpeed * direction;
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
推荐阅读
- python - 无法打开文件“hello_world.py”:[Errno 2] 没有这样的文件或目录
- java - 如何使用 SXSSFWorkbook 打开一个大的 excel 模板
- c++ - 两个迭代器的减法如何表示 C++ 中向量项的索引?
- css - Chrome 字体渲染
- node.js - AWS:reactjs 应用程序的连接被拒绝错误
- c - Sqlite 合并 C 文件无法在 Raspbian 上编译
- swift - SwiftUI onDrag。如何提供多个 NSItemProviders?
- algorithm - 给定元素数组,计算长度为“k”的子序列的数量,这些子序列形成具有给定比率“r”的几何级数
- javascript - vega-lite:可调节的选择?
- css - 水平居中对齐 div 与灵活