我正在开发我的第一款FPS游戏。我将尝试解释到目前为止我所做的工作以及我尝试解决问题的方法。
有一个控制播放器的FPS控制器,并且玩家附有武器。 Weapon游戏对象具有名为ButtetSpown的子对象。我使用平原作为地面,并添加了一些立方体作为墙壁。这是我的基本设置。我通过光线投射收集目标位置。
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
我将这一点指向实例化的子弹。下面是我的实例化进程代码(我已经删除了枪动画代码)
void Update () {
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
//Debug.DrawRay(rayOrigin, fpsCam.transform.forward * weaponRange, Color.green);
if (Input.GetMouseButton(0) && Time.time > NextFire)
{
NextFire = Time.time+ FireRate;
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
bulletclone = Instantiate(Bullet.transform, SpownPoint.transform.position, SpownPoint.transform.rotation) as Transform;
bullet bulletScript = bulletclone.GetComponent<bullet>();
bulletScript.target = hit;
Debug.Log("hit " + hit.point );
bulletScript.Accuracy = Accuracy;
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * 100);
}
}
StartCoroutine( Firing ());
}
}
Bullet脚本更新方法。子弹有严格的身体附着和iskinematic是真的。
Vector3 targetPosition=new Vector3(target.point.x + Random.Range(-Accuracy*2, Accuracy*2), target.point.y + Random.Range(-Accuracy/2, Accuracy/2), target.point.z);
Debug.Log("Target " + targetPosition);
rb.velocity = (targetPosition- transform.position).normalized * speed;
这很好用:子弹正确地获得目标。添加 PoolManager 以控制预制件后,问题就出现了。我写了PoolManager脚本。 PoolManager脚本附加到PoolManager游戏对象。
PoolManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoolManager : MonoBehaviour {
[Tooltip("Maximum size of a gameobject can be in the pool")]
public int MaxPoolSize = 20;
public bool CanGrow = true;
public GameObject[] PoolItems;
public int[] ItemSize;
private GameObject[] ActiveList;
private Dictionary<string, int> Pooled = new Dictionary<string,int>();
void Start () {
//Add default prefabs as child of PoolManager gameobject
for (int i = 0; i < PoolItems.Length; i++)
{
Pooled.Add(PoolItems[i].name ,0);
for (int s = 0; s < ItemSize[i]; s++)
{
GameObject g = Instantiate(PoolItems[i]);
g.name = PoolItems[i].name;
g.transform.parent = transform;
g.SetActive( false);
Pooled[g.name]=s + 1;
}
}
}
public Transform InstantiateObject(int ItemIndex, Vector3 Position, Quaternion Rotation)
{
Transform test = TryActive(ItemIndex, Position, Rotation);
if (CanGrow == true && test == null)
{
if (Pooled[PoolItems[ItemIndex].name] < MaxPoolSize)
{
GameObject g = Instantiate(PoolItems[ItemIndex]);
g.name = PoolItems[ItemIndex].name;
g.transform.parent = transform;
Pooled[g.name] = Pooled[g.name] + 1;
return TryActive(ItemIndex, Position, Quaternion.identity);
}
}
return test;
}
Transform TryActive(int ItemIndex, Vector3 Position, Quaternion Rotation)
{
if(PoolItems.Length==0) return null;
int children = transform.childCount;
for (int i = 0; i < children; ++i)
{
if (transform.GetChild(i).name == PoolItems[ItemIndex].name)
{
if (transform.GetChild(i).gameObject.activeInHierarchy == false)
{
transform.GetChild(i).gameObject.SetActive(true);
transform.GetChild(i).position = Position;
transform.GetChild(i).rotation = Rotation;
return PoolItems[ItemIndex].transform;
}
}
}
}
}
因此,所有实例化的预制件都将作为PoolManager游戏对象的子项添加。
然后我改变了我的枪脚更新功能,如下所示:
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
//1st parameter 0 is my bullet prefab which in PoolManager (PoolItems).
// Only this code has changed from previous version
bulletclone = poolmanager.InstantiateObject(0, SpownPoint.transform.position , SpownPoint.transform.rotation);
bullet bulletScript = bulletclone.GetComponent<bullet>();
bulletScript.target = hit;
Debug.Log("hit " + hit.point );
bulletScript.Accuracy = Accuracy;
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * 100);
}
}
问题是:Bullet正确地在SpownPoint中激活,但子弹不会转到实际目标。它总是到达平原(地面)的(0,0,0)点。我通过调试子弹targetPosition和命中点位置来检查射线命中点和子弹目标位置。
Debug.Log("hit " + hit.point );
Debug.Log("Target " + targetPosition);
有谁能告诉我这里我做错了什么?我应该改变什么?
答案 0 :(得分:0)
我在PoolManager脚本中发现了问题。 TryActive方法的return语句应该从
更改return PoolItems[ItemIndex].transform;
到
return transform.GetChild(i).transform;