【问题标题】:Unity Player is shooting at himselfUnity Player 正在向自己开枪
【发布时间】:2020-02-15 19:28:44
【问题描述】:

我的游戏中有以下问题:

场景中有两个角色,他们可以互相射击。如图所示,在彼此前面附加了一个名为“SpawnBullet”的空游戏对象,它会生成弹丸。

问题在于名为“玩家 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);
                }
            }
        }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 感谢您的帮助,但没有奏效。玩家 2 仍在向自己射击。子弹朝他的方向飞去。
    • 这很奇怪。您正在旋转玩家而不是空的“SpawnBullet”游戏对象,对吗?
    • 我正在旋转“SpawnBullet”游戏对象,但没有任何变化;\
    • 您需要旋转玩家,而不是“SpawnBullet”游戏对象。 transform.forward 引用了脚本附加到的对象,在本例中为播放器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2018-08-18
    相关资源
    最近更新 更多