简体   繁体   English

如何相对于玩家方向改变子弹方向?

[英]How to change bullet direction with respect to player direction?

I've been reading this question for a long time and some of the things they suggest I have them implemented in my code. 我已经阅读了很长时间这个问题,并且他们建议我在代码中实现它们的一些内容。 But I cant still figure it out why is not working properly. 但是我仍然无法弄清楚为什么不能正常工作。 If my player is facing right it shoots to the right but if its facing to the left it shoots to the right. 如果我的玩家朝右,则向右射击,但如果玩家朝左,则向右射击。 My game is in 2D and I have my bullet prefab attach with this script, Im programming in C#. 我的游戏是2D模式,我的子弹预制件附带此脚本,即使用C#进行Im编程。

public class Bullet : MonoBehaviour {


    public float speed;
    public float VelXBala;

    // Use this for initialization
    void Start () {
        rigidbody2D.AddForce (new Vector2 (VelXBala,0), ForceMode2D.Impulse);
    }

    void Update(){
        transform.Translate (1, 0, speed);
    }


    void OnBecameInvisible(){
        Destroy (gameObject);
    }
}

And I have my player script 我有我的播放器脚本

public float speed;
    public Vector2 maxVel;
    public float airSpeed;
    public float jumpSpeed;
    public bool grounded;
    public Transform groundedEnd;
    public float jumpPower = 1;

    public GameObject bulletPrefab;


    private PlayerController controller;
    private Animator animator;


    void Start () {
        controller = GetComponent<PlayerController> ();
        animator = GetComponent<Animator> ();
        maxVel = new Vector2 (3, 5);
        speed = 3f;
        airSpeed = 0.3f;
        jumpSpeed = 300f;
        grounded = false;
    }


    void Movement(){
        Vector2 force = new Vector2(0f, 0f);
        Vector2 absVelocity = new Vector2 (rigidbody2D.velocity.x, rigidbody2D.velocity.y);
        Vector2 currentPosition = new Vector2(rigidbody2D.position.x, rigidbody2D.position.y);

        if (controller.moving.x != 0) {
            if (absVelocity.x < maxVel.x){
                force.x = grounded ? (speed * controller.moving.x) :
                    (speed * airSpeed * controller.moving.x);
                transform.localScale = new Vector2 (controller.moving.x, 1);
            }
            animator.SetInteger ("AnimState", 1);
            transform.Translate(new Vector2(speed*Time.deltaTime*controller.moving.x, 0));
        } else {
            animator.SetInteger("AnimState", 0);
        }

        /*if (controller.moving.y > 0 && grounded) {
            force.y = jumpSpeed;
            rigidbody2D.AddForce(force);
            //animator.SetInteger("AnimState", 2);
        }*/

        if(!grounded && rigidbody2D.velocity.y == 0) {
            grounded = true;
        }
        if (controller.moving.y>0 && grounded == true) {
            rigidbody2D.AddForce(transform.up*jumpPower);
            grounded = false;
            animator.SetInteger("AnimState",2);
        }

        if(Input.GetMouseButtonDown(0)){
            //crea un clon de un objeto
            Instantiate(bulletPrefab,transform.position,transform.rotation);

        }

    }





    void Update () {
        Movement ();

    }


}

You are updating bullet position by translating it (1, 0, 0), so it will always moving to the right. 您正在通过翻译(1、0、0)来更新子弹位置,因此它将始终向右移动。

Supposing you are instantiating the bullet in the right position and orientation: 假设您以正确的位置和方向实例化了项目符号:

Instantiate(bulletPrefab,transform.position,transform.rotation);

You must update your bullet position to move forward: 您必须更新子弹位置才能前进:

void Update(){
    transform.position += Vector3.forward * speed;
}

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

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