【问题标题】:Projectile not shooting direction of my weapon弹丸不是我的武器的射击方向
【发布时间】:2020-01-22 00:27:05
【问题描述】:

所以我的游戏有点像一个 3D 自上而下的射击游戏,所以我希望我的枪在鼠标所在的任何地方射击,除非我击落,否则它不会射向鼠标。如果您看过名为 Ball Wars 的 brackyes 游戏,我会尝试复制一个类似的游戏,但弹丸的射击方式不正确。

我从 blackthornprods 远程战斗教程中获得了我的脚本(这是 2d 的,所以也许这就是问题,但我不知道如何解决它):

public float speed;
public float lifeTime;

private void Start()
{
    Invoke("DestoryProjectile", lifeTime);
}

private void Update()
{
    transform.Translate(transform.up * speed * Time.deltaTime);
}

void DestroyProjectile()
{
    Destroy(gameObject);
}

感谢任何人尝试!

这是我的另一个脚本: 摄像头主摄像头;

public GameObject projectile;

public Transform shotPoint;

private float timeBtwShots;
public float startTimeBtwShots;

void Awake()
{
    mainCam = Camera.main;
}

void Update()
{
    float objectDepthFromCamera = Vector3.Dot(
            transform.position - mainCam.transform.position,
            mainCam.transform.forward);

    Vector3 cursorWorldPosition = mainCam.ScreenToWorldPoint(Input.mousePosition
            + Vector3.forward * objectDepthFromCamera);

    Vector3 localUpNeeded = Vector3.Cross(Vector3.forward,
            cursorWorldPosition - transform.position);

    transform.rotation = Quaternion.LookRotation(Vector3.forward, localUpNeeded);

    if(timeBtwShots <= 0)
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(projectile, shotPoint.position, transform.rotation);
            timeBtwShots = startTimeBtwShots;
        } 
    }
    else
    {
        timeBtwShots -= Time.deltaTime;
    }
}

【问题讨论】:

  • 您是否尝试在代码的其他任何地方添加Input.mousePosition,或者这是您的整个射弹类?
  • 问题在于产生子弹的代码。我们需要那个代码。
  • 我编辑了我的问题,你应该在那里找到它

标签: c# unity3d bullet


【解决方案1】:

弹丸不是我武器的射击方向。简单的解决方案 -

  • 首先实例化或池化 projectile 的实例。
  • 设置投影旋转从武器旋转和设置位置到生成点
  • 现在开火,或者你正在使用的任何策略

考虑全局轮换,如果你需要帮助,告诉我,我会编辑并给出一个sn-p的代码。

这应该可行。如果没有发布所有必要的代码,我会给出更好的解决方案。

Here 是我为您创建的示例 github 项目。将近一年后,我打开了 Unity。请检查所有versions

必须检查:

firing in facing direction ? 
just instantiate at spawn point
just added some rotation

我认为这应该给你概念。

Press X for a rantom rotation
Press Space to shoot a projectile :lol:
The white cube shows that it always shoots at a constant direction

【讨论】:

  • 自从我 2 个月前开始统一以来,我不确定如何执行此操作,我将编辑我的问题并将脚本放入实例化射弹,请您编辑并告诉我该怎么做你在上面说什么
  • 我会尽快添加示例项目
  • @Moodie 我已经更新了答案并添加了 github 项目链接,不要忘记点赞和接受答案,如果遇到更多问题可以给我发邮件...
  • 好的,谢谢,非常感谢!我会尽快解决的!
  • 我实际上并不知道如何使用 git hub,所以你能解释一下我应该用那个链接做什么吗?大声笑,我觉得自己很愚蠢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多