【问题标题】:How to make Bullet initiate from the direction the player is in如何让子弹从玩家所在的方向开始
【发布时间】:2016-09-07 16:56:14
【问题描述】:

我正在使用 Unity 制作 2D 游戏。

在此我想添加一个子弹数量有限的子弹。

子弹向玩家所在的方向发射,但始终从右侧发射,即使玩家面向左侧也是如此。而且我已将子弹数限制为 3。

如何在子弹发生之间延迟?

第一个脚本(项目符号)

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

第二个脚本(播放器)

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

第三个脚本(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;
    }
}

【问题讨论】:

    标签: c# unity3d 2d


    【解决方案1】:

    1) 有一种简单的方法可以知道子弹的位置和射击方向。在您的角色下添加一个子虚拟游戏对象,该对象将用作子弹的初始位置。将其放置在您想要的位置。现在这个游戏对象相对于你的角色移动和旋转。实例化项目符号时使用 transform.positiontransform.rotation.forward

    2) 保持当前用户在private float lastShotTime; 等变量中发射子弹的时间。当您发射子弹lastShotTime = Time.time 时更新它的值。然后当用户想再射一颗子弹时,检查距离上一次射击是否已经过去了足够的时间if (Time.time &gt; lastShotTime + fireDelay) { Shoot(); }

    【讨论】:

    • 我正确地得到了子弹延迟的东西,但是得到了一个参数异常,即 get_time 只能从主线程调用并且在位置一,我无法找到玩家面临的当前面并得到一个
    • 通过将 Time.time 放置在函数中来消除异常,但不知道为什么需要这样做。
    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多