c# - 根据鼠标y位置射击子弹?
问题描述
任何帮助深表感谢!
我有一个角色正在射击。我在运行时实例化一个子弹,并将其位置/旋转设置为与世界中的“生成点”相同。我将如何让子弹“击中”鼠标指向的地方,而不是在射击时直接向前移动?
//Bullet instaniate code
bulletSpawned = Instantiate(bullet.transform,
bulletSpawnPoint.transform.position,
bulletSpawnPoint.transform.rotation);
bulletSpawned.rotation = bulletSpawnPoint.transform.rotation;
//Bullet move code
transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed); maxDistance += 1 * Time.deltaTime;
if (maxDistance >= bulletFlyTime)
{
Destroy(this.gameObject);
}
解决方案
谢谢-您的评论使我陷入困境,并找到了适合我的解决方案。下面是我使用的代码。
RaycastHit hit;
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray2, out hit))
{
Debug.Log("The ray hit at: " + hit.point);
bulletSpawnPoint.transform.LookAt(hit.point);
}
推荐阅读
- reactjs - 在大型项目中使用 Redux Saga
- json - Scala:如何使用 ReactiveMongo 查询的 Date 字段映射嵌套案例类?
- java - 使类公开,但对 JAR 文件保持私有
- mysql - 在网格单元格值更新时更新数据库表
- java - 我们如何在同一个 Kafka 消费者中收听多个主题?
- python - 从字符串中提取以 TICKET# 开头的 substr
- c - 为什么 open() 在 linux 1.0 中没有“fd”返回?
- c# - 如何检查远程数据库MYSQL是否可用
- javascript - Spotfire 交叉表可视化格式
- python - 使用 cx_Freeze 创建 exe 时出现 ValueError,包括 PyQt5