在两个角度之间实例化多个游戏对象

我试图在等距的角度之间生成n 个游戏对象。

理想情况下,我希望能够调整“圆锥体”,以便敌人可以以任何密度向任何方向射击。

有人可以看到我做错了什么吗?

https://img1.mukewang.com/64e1845f0001bf3b03920345.jpg

这些是敌人的射弹。我正在尝试“分散射击”。想想 NES 塞尔达传说中第 1 级的龙:

https://img.mukewang.com/64e1846b00010bc704960346.jpg

不过,我并不完全确定我的实施发生了什么。


https://img3.mukewang.com/64e1847500010a6205160351.jpg

弹丸.cs


    public Vector2 moveDirection = Vector2.zero;

    public float moveSpeed = 4.0f;


    private void FixedUpdate()

    {

        _body.MovePosition(transform.position + (new Vector3(moveDirection.x, moveDirection.y, 0).normalized) * (moveSpeed * Time.deltaTime));

    }

多射手.cs


public GameObject projectileObject;

public Transform projectileEmitter;

[Range(2, 10)] public int numToShoot = 3;

[Space]

[Range(0, 360)] public int angle = 30;

[Range(1, 50)]  public float rayRange = 10.0f;

[Range(0, 360)] public float coneDirection = 180;

public void OnStartShooting()

{

    for (int i = 1; i <= numToShoot; i++)

    {

        var projectile = Instantiate(projectileObject);

        projectile.transform.position = projectileEmitter.position;

        var projectileScript = projectile.GetComponent<Projectile>();

        projectileScript.moveDirection = DirFromAngle(((angle / i) + coneDirection)* pointDistance, rayRange);

        projectile.SetActive(true);

    }

}

public Vector3 DirFromAngle(float angleInDegrees, float range)

{

   return Quaternion.AngleAxis(angleInDegrees, Vector3.forward) * transform.up * range;

}

显示线条的编辑器脚本。


private void OnSceneGUI()

    {

        MultiShooter fow = (MultiShooter)target;

        Handles.color = Color.magenta;


        Vector3 upDirection = fow.DirFromAngle((-fow.angle / 2.0f) + fow.coneDirection, fow.rayRange);

        Vector3 dwDirection = fow.DirFromAngle((fow.angle / 2.0f) + fow.coneDirection, fow.rayRange);


        Handles.DrawLine(fow.projectileEmitter.position, upDirection);

        Handles.DrawLine(fow.projectileEmitter.position, dwDirection);


    }


慕的地10843
浏览 82回答 1
1回答

白猪掌柜的

对于i第一个对象,从范围的一侧到另一侧的角距离分数可以用公式 i/(numToShoot-1) 表示,其中 numToShoot > 1 的值。如果 numToShoot == 1,则可以只得到百分比50% 即可在射程正中射击。您的绘图方法似乎适用于 coneDirection ± angle/2,因此我们可以从该角度百分比中减去 0.5,以用距范围中心的角距离来表示它。然后我们可以使用与绘图方法相同的数学:coneDirection + 角度百分比 * 角度范围:public void OnStartShooting(){&nbsp; &nbsp; for (int i = 0; i < numToShoot; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var projectile = Instantiate(projectileObject);&nbsp; &nbsp; &nbsp; &nbsp; projectile.transform.position = projectileEmitter.position;&nbsp; &nbsp; &nbsp; &nbsp; var projectileScript = projectile.GetComponent<Projectile>();&nbsp; &nbsp; &nbsp; &nbsp; float anglePercentage;&nbsp; &nbsp; &nbsp; &nbsp; if (numToShoot == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anglePercentage = 0f;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anglePercentage = (float)i/(numToShoot-1f) - .5f;&nbsp; &nbsp; &nbsp; &nbsp; projectileScript.moveDirection = DirFromAngle(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coneDirection&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + anglePercentage * angle, rayRange);&nbsp; &nbsp; &nbsp; &nbsp; projectile.SetActive(true);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP