gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:17 25 4
gpt4 key购买 nike

我的游戏出现以下问题:

场景中有两个角色,他们可以互相射击。有一个空的游戏对象彼此相连,称为“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com