简体   繁体   English

我怎样才能使预制件从生成点完全反转(180度)? Unity 2D

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

So i made a little side scroller that uses a prefab to spawn bullets. 因此,我制作了一个小侧面滚动条,该滚动条使用了预制件来生成子弹。 My problem is that it only shoots to one side... the Right. 我的问题是它只向一侧射击...正确。
I need it to fire to the left as well. 我也需要它向左发射。 I've already made a variable to see if the player is looking to the right or left. 我已经做了一个变量,看玩家是向右看还是向左看。

I've tried to put the Speed to -20 and i've tried to rotate it 180 degrees on it's Z axis. 我尝试将Speed设置为-20,并尝试将其在Z轴上旋转180度。 I tested if the bullet script even picked up the change from the player movement script and it does. 我测试了子弹脚本是否甚至从玩家移动脚本中获取了更改,并且确实如此。

Player Movement script 玩家移动脚本

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


}

Weapon script 武器脚本

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

bullet script 项目符号脚本

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

As i previously said i need my bullet (prefab) to go the opposite direction but whatever i do right now it will always go right. 正如我之前所说,我需要我的子弹(预制件)朝相反的方向前进,但是无论我现在做什么,它总是会正确的。

Expanding on my comment: 扩展我的评论:

Did you try reversing the velocity? 您是否尝试过反转速度?

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

Note: There are instances when using a negative float wont return the opposite of the positive result. 注意:在某些情况下,使用负浮点数不会返回与正数相反的结果。 However, in this example, it will work just fine. 但是,在此示例中,它将正常工作。

I imagine this will work also ( and is probably the correct way of doing it ): 我想这也将起作用( 并且可能是正确的方法 ):

rb.velocity = transform.left * speed;

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

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