Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成炸弹并将其扔给统一中的玩家_C#_Unity3d - Fatal编程技术网

C# 生成炸弹并将其扔给统一中的玩家

C# 生成炸弹并将其扔给统一中的玩家,c#,unity3d,C#,Unity3d,我有一个小竞技场,玩家可以继续前进。在该区域的两侧有产卵器。这些产卵者实例化炸弹,并向玩家投掷 public class BombSpawner : MonoBehaviour { [Range(0, 3)] [SerializeField] private int nextPointIndex; // set the first targetpoint private Vector3[] targetPoints = { new Vector3

我有一个小竞技场,玩家可以继续前进。在该区域的两侧有产卵器。这些产卵者实例化炸弹,并向玩家投掷

public class BombSpawner : MonoBehaviour
{
    [Range(0, 3)]
    [SerializeField]
    private int nextPointIndex; // set the first targetpoint

    private Vector3[] targetPoints = {
        new Vector3(-15,0,15),
        new Vector3(15,0,15),
        new Vector3(15,0,-15),
        new Vector3(-15,0,-15)};

    private float movementSpeed = 10;

    GameObject bombPrefab;

    Transform player;

    private void Start()
    {
        bombPrefab = Resources.Load(StringCollection.BOMB) as GameObject;
        player = Globals.GetPlayerObject().transform;
    }

    private void Update()
    {
        transform.LookAt(player); // set the object rotation

        Vector3 nextPoint = targetPoints[nextPointIndex]; // get the target point
        transform.position = Vector3.MoveTowards(transform.position, nextPoint, movementSpeed * Time.deltaTime); // move the spawner
        if (transform.position == nextPoint) // point reached? set a new point 
        {
            if (nextPointIndex < targetPoints.Length - 1)
                nextPointIndex++;
            else
                nextPointIndex = 0;
        }
    }
}
对于我实际使用的方向

transform.lookAt(playerttransform)

这是一张粗略的地图

因此,产卵器正在围绕地图旋转。它们从一个点移动到下一个点

我的炸弹物体附着了一个刚体,重力被激活了。我只是想知道如何让产卵者向玩家投掷炸弹

public class BombSpawner : MonoBehaviour
{
    [Range(0, 3)]
    [SerializeField]
    private int nextPointIndex; // set the first targetpoint

    private Vector3[] targetPoints = {
        new Vector3(-15,0,15),
        new Vector3(15,0,15),
        new Vector3(15,0,-15),
        new Vector3(-15,0,-15)};

    private float movementSpeed = 10;

    GameObject bombPrefab;

    Transform player;

    private void Start()
    {
        bombPrefab = Resources.Load(StringCollection.BOMB) as GameObject;
        player = Globals.GetPlayerObject().transform;
    }

    private void Update()
    {
        transform.LookAt(player); // set the object rotation

        Vector3 nextPoint = targetPoints[nextPointIndex]; // get the target point
        transform.position = Vector3.MoveTowards(transform.position, nextPoint, movementSpeed * Time.deltaTime); // move the spawner
        if (transform.position == nextPoint) // point reached? set a new point 
        {
            if (nextPointIndex < targetPoints.Length - 1)
                nextPointIndex++;
            else
                nextPointIndex = 0;
        }
    }
}

但是我怎样才能达到投掷技巧呢?第一次尝试时,目标点是玩家。位置应该可以。

你需要获得从繁殖者到目标当前位置的方向,繁殖炸弹,并使用刚刚获得的方向向炸弹添加力量

为了做到这一点,你应该从你的目标位置减去你的产卵者的位置

Vector3 dir = target.transform.position - transform.position;
现在你有了方向,你可以生成炸弹并
AddForce()
。要添加力,需要调用衍生炸弹的刚体组件,如下所示:

spawnedBomb.GetComponent<Rigidbody>().AddForce(dir.normalized * force, ForceMode.Impulse);
sprownedbomb.GetComponent().AddForce(dir.normalized*force,ForceMode.pulse);
其中
dir
是指向目标的方向(标准化-因此距离无关紧要),而
force
几乎是炸弹的速度


在这里,您可以阅读更多信息。

您需要获得从“繁殖者”到目标当前位置的方向,繁殖炸弹,并使用刚刚获得的方向向该炸弹添加力

为了做到这一点,你应该从你的目标位置减去你的产卵者的位置

Vector3 dir = target.transform.position - transform.position;
现在你有了方向,你可以生成炸弹并
AddForce()
。要添加力,需要调用衍生炸弹的刚体组件,如下所示:

spawnedBomb.GetComponent<Rigidbody>().AddForce(dir.normalized * force, ForceMode.Impulse);
sprownedbomb.GetComponent().AddForce(dir.normalized*force,ForceMode.pulse);
其中
dir
是指向目标的方向(标准化-因此距离无关紧要),而
force
几乎是炸弹的速度


您可以在这里了解更多信息。

此答案应该是公认的答案。只有一个输入错误:target.transform.position-transform.position是的,我改变了炸弹的阻力和质量,看起来不错!这个答案应该是可以接受的。只有一个输入错误:target.transform.position-transform.position是的,我改变了炸弹的阻力和质量,看起来不错!