我怎样才能使我的预制件从它的出生点完全反转(180 度)?统一二维

How can i make my prefab completely reverse (180 degrees) from it's spawnpoint? Unity 2D

所以我制作了一个小侧边卷轴,它使用预制件来生成子弹。 我的问题是它只射向一侧...右侧。
我也需要它向左开火。我已经制作了一个变量来查看玩家是在向右还是向左看。

我尝试将速度设置为 -20,并尝试将其在 Z 轴上旋转 180 度。我测试了项目符号脚本是否甚至从玩家移动脚本中获取了更改并且确实如此。

玩家移动脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public GameObject bullet;

    private Rigidbody2D myRigidbody;
    private float speed = 15;
    private bool facingRight;
    private bool ground = false;
    private float jump = 23;
    // Start is called before the first frame update
    void Start()
    {
        facingRight = true;
        myRigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");

        Movement(horizontal);
        Flip(horizontal);
        if (Input.GetKey("w"))
        {
            if (ground)
            {
                GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
            }
        }
        if(facingRight == false)
        {
            bullet.GetComponent<bullet>().left = true;
        }
        if (facingRight == true)
        {
            bullet.GetComponent<bullet>().left = false;
        }
    }

    void OnTriggerEnter2D()
    {
        ground = true;
    }

    void OnTriggerExit2D()
    {
        ground = false;
    }

    private void Movement(float horizontal)
    {

        myRigidbody.velocity = new Vector2(horizontal * speed,myRigidbody.velocity.y);
    }

    private void Flip(float horizontal)
    {
        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;
        }
    }


}

武器脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class weapon : MonoBehaviour
{
    // Start is called before the first frame update
    public bool right;
    public Transform firepointR;

    public GameObject bulletPrefab;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            Debug.Log("Oh well");
            Shoot();
        }

    }

    void Shoot()
    {
            Instantiate(bulletPrefab, firepointR.position, firepointR.rotation);
    }
}

子弹脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bullet : MonoBehaviour
{
    public bool left;
    public float speed = 20;

    public Rigidbody2D rb;
    // Start is called before the first frame update
    void Start()
    {
        rb.velocity = transform.right * speed;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Debug.Log(speed);

        if (left == false)
        {
            transform.Rotate(0, 0, 180);
        }
    }
}

正如我之前所说,我需要我的子弹(预制件)朝相反的方向前进,但无论我现在做什么,它总是会朝正确的方向前进。

扩展我的评论:

你试过反转速度吗?

rb.velocity = -(transform.right * speed);

注意:在某些情况下,使用负浮点数不会 return 与正结果相反。然而,在这个例子中,它会工作得很好。

我想这也会起作用(并且可能是正确的方法):

rb.velocity = transform.left * speed;