首页 > 解决方案 > 根据鼠标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);
    }

标签: c#unity3d

解决方案


谢谢-您的评论使我陷入困境,并找到了适合我的解决方案。下面是我使用的代码。

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

推荐阅读