繁体   English   中英

射弹不是我武器的射击方向

[英]Projectile not shooting direction of my weapon

所以我的游戏有点像 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);
}

感谢任何人尝试!

这是我的另一个脚本:Camera mainCam;

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

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

  • 首先实例化或池化弹丸的实例。
  • 从武器旋转设置投影旋转并将位置设置为重生点
  • 现在开火,或者你正在使用的任何策略

考虑全局轮换,如果您需要帮助,请告诉我,我将编辑并提供一段代码。

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

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

必须检查:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM