对角射击子弹

时间:2018-04-25 13:11:46

标签: c# unity3d game-physics

我有一艘宇宙飞船直接射击。这项工作到目前为止。但是我想要一些对角移动的镜头。

这是子弹脚本:

public float damage = 25f;

float speed;

// Use this for initialization
void Start () {
    speed = 8f;
}

// Update is called once per frame
void Update () {
    //get the bullet's current position
    Vector2 position = transform.position;

    //compute the bullet's new position
    position = new Vector2(position.x, position.y + speed * Time.deltaTime);

    //update the bullet's position
    transform.position = position;

    //this is the top right point of the screen
    Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

    //if the bullet went outside the screen on the top, then destroy the bullet
    if (transform.position.y > max.y) {
    // this is just an object-pool
        PlayerControl.bulletPool.ReturnInstance(gameObject);
    }
}

这是playerhoot-script:

public GameObject bulletPosition01;
public GameObject playerShotsGO;

void Update () {
    //fire bullets when the spacebar is pressed
    if (Input.GetKeyDown ("space")) {
        Shoot();
    }

//function to make the player shoot
public void Shoot () {
    //play the laser sound effect
    SoundManager.PlaySFX (shootAudio);

    //get bullet from object-pool
    GameObject bullet01 = bulletPool.GetInstance (playerShotsGO);
    bullet01.transform.position = bulletPosition01.transform.position;

    bullet01.transform.SetParent (playerShotsGO.transform, true);
}

1 个答案:

答案 0 :(得分:3)

这不是如何正确拍摄预制件。为此使用内置物理。如果它是3D对象,请将Rigidbody附加到GameObject。如果它是2D对象,请将Rigidbody2D附加到它。您可以使用Rigidbody.AddForceRigidbody.velcociy来拍摄对象。为了使其朝向它所朝向的方向,使用transform.forward并通过一些力量将其倍增。

一个非常基本的2D拍摄脚本:

public float speed = 100;
//Assign from the Editor
public GameObject bulletPrefab;


void Update()
{
    if (Input.GetKeyDown("space"))
    {
        GameObject bullet = Instantiate(bulletPrefab);
        ShootBullet(bullet);
    }
}

void ShootBullet(GameObject rb)
{
    Rigidbody2D bulletRb = rb.GetComponent<Rigidbody2D>();

    //The direction to shoot the bullet
    Vector3 pos = bulletRb.transform.forward * speed;
    //Shoot
    bulletRb.velocity = pos;
}

作为“Gunnar B” mentioned,如果从宇宙飞船上射出多个(2)子弹,那么在宇宙飞船的侧面创建两个空物体,你希望子弹从。还将这两个对象放在宇宙飞船下,然后将它们用作实例化子弹的位置。

public float speed = 100;
//Assign bulletPrefab from the Editor
public GameObject bulletPrefab;
//Assign ship from the Editor
public Transform spaceShip;

//[Empty GameObject] Assign from the Editor
public Transform leftBarrel;
//[Empty GameObject] Assign from the Editor
public Transform rightBarrel;


void Update()
{
    if (Input.GetKeyDown("space"))
    {
        //Instantiate left and right bullets
        GameObject leftBullet = Instantiate(bulletPrefab, leftBarrel.position, spaceShip.rotation);
        GameObject rightBullet = Instantiate(bulletPrefab, rightBarrel.position, spaceShip.rotation);

        //Shoot left and right bullets
        ShootBullet(leftBullet);
        ShootBullet(rightBullet);
    }
}

void ShootBullet(GameObject obj)
{
    Rigidbody2D bulletRb = obj.GetComponent<Rigidbody2D>();

    //The direction to shoot the bullet
    Vector3 pos = spaceShip.up * speed;
    //Shoot
    bulletRb.velocity = pos;
}