简体   繁体   English

如何使子弹从玩家所在的方向开始

[英]How to make Bullet initiate from the direction the player is in

I am making 2D game in Unity. 我正在Unity中制作2D游戏。

In this I wanted to add a bullet with a limited number of shots. 在此,我想添加数量有限的子弹。

The bullet fires in the direction in which the player is but always initiate from the right side even if the player is facing left side. 子弹朝着玩家所在的方向射击,但即使玩家面对左侧也总是从右侧发射。 And I have limited the bullet count to 3. 而且我将子弹数限制为3。

How do I put delay in between the occurrence of the bullets? 如何在子弹出现之间延迟?

1st Script (Bullet) 第一个剧本(子弹)

public class Bullet : MonoBehaviour {

    private Player player;
    public float speed = 1f;
    public int abc = 2;

    // Use this for initialization
    void Start () {
        player = GameObject.Find ("Player").GetComponent<Player> ();
        if (player.aa.x == transform.localScale.x)
            abc = 1;
    }

    // Update is called once per frame
    public void Update () {
        if (abc == 1)
            rigidbody2D.velocity = new Vector3 (transform.localScale.x, 0, 1) * speed;
        else
            rigidbody2D.velocity = new Vector3 (transform.localScale.x, 0, 1) * speed;
    }
}

2nd Script (Player) 第二剧本(玩家)

public class Player : MonoBehaviour {

    public float speed = 10f;
    public Vector2 maxVelocity = new Vector2(3, 5);
    public bool standing;
    public float jetSpeed = 15f;
    public float airSpeedMultiplier = .3f;
    public AudioClip leftFootSound;
    public AudioClip rightFootSound;
    public AudioClip thudSound;
    public AudioClip rocketSound;
    public Vector3 aa = new Vector3(1,1,1);

    private Animator animator;
    private PlayerController controller;

    void Start(){
        controller = GetComponent<PlayerController> ();
        animator = GetComponent<Animator> ();
    }

    void PlayLeftFootSound(){
        if (leftFootSound)
            AudioSource.PlayClipAtPoint (leftFootSound, transform.position);
    }

    void PlayRightFootSound(){
        if (rightFootSound)
            AudioSource.PlayClipAtPoint (rightFootSound, transform.position);
    }

    void PlayRocketSound(){
        if (!rocketSound || GameObject.Find ("RocketSound"))
            return;

        GameObject go = new GameObject ("RocketSound");
        AudioSource aSrc = go.AddComponent<AudioSource> ();
        aSrc.clip = rocketSound;
        aSrc.volume = 0.7f;
        aSrc.Play ();

        Destroy (go, rocketSound.length);
    }

    void OnCollisionEnter2D(Collision2D target){
        if (!standing) {
            var absVelX = Mathf.Abs(rigidbody2D.velocity.x);
            var absVelY = Mathf.Abs(rigidbody2D.velocity.y);

            if(absVelX <= .1f || absVelY <= .1f){
                if(thudSound)
                    AudioSource.PlayClipAtPoint(thudSound, transform.position);
            }
        }
    }

    // Update is called once per frame
    void Update () {
        var forceX = 0f;
        var forceY = 0f;

        var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
        var absVelY = Mathf.Abs (rigidbody2D.velocity.y);

        if (absVelY < .2f)
            standing = true;
        else
            standing = false;

        if (controller.moving.x != 0) {
            if (absVelX < maxVelocity.x) {

                forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeedMultiplier);

                aa = transform.localScale = new Vector3 (forceX > 0 ? 1 : -1, 1, 1);

            }

            animator.SetInteger ("AnimState", 1);

        } else {
            animator.SetInteger ("AnimState", 0);
        }

        if (controller.moving.y > 0) {
            PlayRocketSound();
                        if (absVelY < maxVelocity.y)
                                forceY = jetSpeed * controller.moving.y;

                        animator.SetInteger ("AnimState", 2);
                } else if (absVelY > 0) {
            animator.SetInteger("AnimState", 3);
                }

        rigidbody2D.AddForce (new Vector2 (forceX, forceY));
    }
}

3rd Script (PlayerController) 第三脚本(PlayerController)

public class PlayerController : MonoBehaviour {

    public Vector2 moving = new Vector2();
    public int Bulletlimit = 0;
    public int MaxBulletlimit = 3;
    public float bulletDelay = 3f;
    public bool Gun;

    public Bullet bullet;
    // Use this for initialization
    void Start () {}

    // Update is called once per frame
    void Update () {

        moving.x = moving.y = 0;

        if (Input.GetKey ("right")) {
            moving.x = 1;
        } else if (Input.GetKey ("left")) {
            moving.x = -1;
        }

        if (Input.GetKey ("up")) {
            moving.y = 1;
        } else if (Input.GetKey ("down")) {
            moving.y = -1;
        }

        if (Input.GetKey ("s")) {
            if(Gun){
                if(Bulletlimit < MaxBulletlimit)
                {
                    Bullet clone = Instantiate (bullet, transform.position, Quaternion.identity) as Bullet;
                    Bulletlimit = Bulletlimit + 1;
                }
            }
        }   
    }

    public void BulletCount() {
        Bulletlimit = Bulletlimit - 1;
    }
}

1) There is an easy method to know where to position your bullets and which direction to shoot. 1)有一种简单的方法可以知道子弹的位置和射击方向。 Add a child dummy gameobject under your character that will be used as bullet's initial position. 在您的角色下添加一个子虚拟游戏对象,该子对象将用作项目符号的初始位置。 Position it where you want. 将其放置在您想要的位置。 Now this gameobject moves and rotates relative to your character. 现在,该游戏对象相对于您的角色移动和旋转。 Use it's transform.position and transform.rotation.forward when you instantiate bullets. 实例化项目符号时,使用它的transform.positiontransform.rotation.forward

2) Keep current time when user fired a bullet in a variable like private float lastShotTime; 2)保留用户在子弹中向诸如private float lastShotTime;类的变量发射项目符号时的当前时间private float lastShotTime; . Update it's value when you fired a bullet lastShotTime = Time.time . 发射子弹lastShotTime = Time.time时更新其值。 Then when user wants to shoot another bullet, check if enough time has passed since last shot if (Time.time > lastShotTime + fireDelay) { Shoot(); } 然后,当用户想要发射另if (Time.time > lastShotTime + fireDelay) { Shoot(); }子弹时,请检查自上次射击以来是否经过了足够的时间, if (Time.time > lastShotTime + fireDelay) { Shoot(); } if (Time.time > lastShotTime + fireDelay) { Shoot(); } . if (Time.time > lastShotTime + fireDelay) { Shoot(); }

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

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