c# - Unity Player 正在向自己射击

标签 c# unity3d

我的游戏出现以下问题:

场景中有两个角色,他们可以互相射击。有一个空的游戏对象彼此相连,称为“SpawnBullet”,它会产生射弹,如图所示。

enter image description here

问题是名为“Player 2”的游戏对象正在向自己开枪,子弹朝他的方向飞去。即使我旋转 SpawnBullet。在播放器 1 中它工作正常。

此脚本附在玩家身上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Moviment : MonoBehaviour
{
    //player variables
    public GameObject player;
    public GameObject[] Personagens;

    //moving variables
    Vector3 targetPosition;
    float posY = 1;
    public float velocity = 0.2f;
    public float movMax = 3;
    public bool ismoving = false;
    public bool moveEnabled;
    public int aux;

    //bullet variables
    public GameObject projetil;
    private GameObject SpawBala;
    public float ProjetilVeloc = 500f;

    private void Start()
    {
        //sets the first unit as the active unit at the start of the game
        Personagens[0].GetComponent<Jogador>().isPlayer = true;
        TurnStart();

    }

    // Update is called once per frame
    void Update()
    {
        //left mouse button to start movement
        if (Input.GetMouseButtonDown(0))
        {
            //raycast checks and returns an object, if it's a tile, the unit moves
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            ismoving = true;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.tag == "Tile")
                {
                    //checks if the tile is available based on the max movement of the unit
                    Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
                    Jogador scriptJog = player.GetComponent<Jogador>();
                    if ((tileAux.TilePostion.x - scriptJog.GridPosition.x) + (tileAux.TilePostion.y - scriptJog.GridPosition.y) >= -movMax && (tileAux.TilePostion.x - scriptJog.GridPosition.x) + (tileAux.TilePostion.y - scriptJog.GridPosition.y) <= movMax)
                    {
                        if ((tileAux.TilePostion.x - scriptJog.GridPosition.x) - (tileAux.TilePostion.y - scriptJog.GridPosition.y) >= -movMax && (tileAux.TilePostion.x - scriptJog.GridPosition.x) - (tileAux.TilePostion.y - scriptJog.GridPosition.y) <= movMax)
                        {
                            targetPosition = (hit.transform.position);
                            targetPosition.y = posY;
                            moveEnabled = true;
                        }

                    }
                }
            }

        }

        //right click to shoot
        if (Input.GetMouseButtonDown(1))
        {
            //raycast checks and returns an object, if it's a tile, the unit shoots
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                //checks if the tile is available based on the line and column of the unit
                Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
                Jogador scriptJog = player.GetComponent<Jogador>();

                if (tileAux.TilePostion.x == scriptJog.GridPosition.x || tileAux.TilePostion.y == scriptJog.GridPosition.y)
                {
                    if (tileAux.TilePostion.x > scriptJog.GridPosition.x)
                        tileAux.TilePostion.x = 5;
                    else
                        tileAux.TilePostion.x = 0;

                    if (tileAux.TilePostion.y > scriptJog.GridPosition.y)
                        tileAux.TilePostion.y = 5;
                    else
                        tileAux.TilePostion.y = 0;

                    Debug.Log(tileAux.TilePostion.x);
                    Debug.Log(tileAux.TilePostion.y);

                    //instantiates the bullet
                    GameObject tiro = Instantiate(projetil, SpawBala.transform.position, Quaternion.identity, player.transform);
                    Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
                    BalaRigid.AddForce(Vector3.forward * ProjetilVeloc);
                }
            }
        }

        //player moves until reaches the position
        if (player.transform.position != targetPosition)
        {
            player.transform.position = Vector3.MoveTowards(player.transform.position, targetPosition, velocity);
            if (player.transform.position == targetPosition)
                ismoving = false;
        }

        //if player reaches position, it's deselected
        if (moveEnabled && !ismoving)
        {
            player.GetComponent<Jogador>().isPlayer = false;
            moveEnabled = false;
        }
    }

    public void TurnStart()
    {
        //makes the selected unit the active unit
        for (int i = 0; i < Personagens.Length; i++)
        {
            if (Personagens[i].GetComponent<Jogador>().isPlayer == true)
            {
                player = Personagens[i];
                posY = player.transform.position.y;
                targetPosition = player.transform.position;
                SpawBala = player.transform.GetChild(0).gameObject;
            }
        }
    }

    public void TurnEnd()
    {
        //desactivates all units
        for(int i = 0; i < Personagens.Length; i++)
        {
            Personagens[i].GetComponent<Jogador>().isPlayer = false;
        }
    }
}

我正在使用这个部分(从上面复制)来拍摄:

//right click to shoot
        if (Input.GetMouseButtonDown(1))
        {
            //raycast checks and returns an object, if it's a tile, the unit shoots
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                //checks if the tile is available based on the line and column of the unit
                Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
                Jogador scriptJog = player.GetComponent<Jogador>();

                if (tileAux.TilePostion.x == scriptJog.GridPosition.x || tileAux.TilePostion.y == scriptJog.GridPosition.y)
                {
                    if (tileAux.TilePostion.x > scriptJog.GridPosition.x)
                        tileAux.TilePostion.x = 5;
                    else
                        tileAux.TilePostion.x = 0;

                    if (tileAux.TilePostion.y > scriptJog.GridPosition.y)
                        tileAux.TilePostion.y = 5;
                    else
                        tileAux.TilePostion.y = 0;

                    Debug.Log(tileAux.TilePostion.x);
                    Debug.Log(tileAux.TilePostion.y);

                    //instantiates the bullet
                    GameObject tiro = Instantiate(projetil, SpawBala.transform.position, Quaternion.identity, player.transform);
                    Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
                    BalaRigid.AddForce(Vector3.forward * ProjetilVeloc);
                }
            }
        }

最佳答案

当您向子弹添加力时,您使用的是 Vector3.forward。您需要改用 transform.forward

Vector3.forward 始终是常量 (0, 0, 1)。把它想象成你世界的前进方向。它永远不会改变。

transform.forward 但是会给出游戏对象(玩家)的前向方向。当你旋转你的播放器时,它的 transform.forward 会相应地改变。

Player 1 似乎工作正常的原因是因为它面向与 Vector3.forward 相同的方向。

关于c# - Unity Player 正在向自己射击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456964/

相关文章:

c# - 使 MouseWheel 事件从子级传递到父级

c# - Unity for Android - 对象不跟随触摸输入

unity3d - 如何通过代码访问 Unity 的 TextMesh Pro Dropdown 组件?

c# - 给对象唯一的 ID onStart

c# - 更改 UI RectTransform 位置

ios - iOS 构建中未显示 Unity 调试日志

c# - 无法使用 Excel Interop 安排程序

c# - 使用线程会节省时间吗?

c# - 将集合写入 Excel

c# - 从 web.config 中的配置部分解析 bool 值