0
votes

I am developing my first FPS game. I'll try to explain what I have done so far and what I did try to solve my problem.

There is a FPS controller that controls the player and there is a weapon attached to the player. The Weapon game object has child object called ButtetSpown. I used a plain as ground and some cubes added as walls. That is my basic setup. I'm collecting target position by Ray-casting.

Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

and I give this point to an Instantiated bullet. Below is my instantiate process code (I have removed the gun animation code)

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 script update method. bullet have rigid body attach and iskinematic is true.

 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;

This works fine: the bullet gets its target correctly. The problem happens after I add PoolManager to control prefabs. I wrote the PoolManager script. The PoolManager script is attached to a PoolManager gameobject.

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


    }

}

So all instantiated prefabs will add as children of the PoolManager gameobject.

Then I changed my Gun script update function as below:

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

                    }
     }

Here is the problem: Bullet activates in SpownPoint correctly but the bullet does not go to the actual target. It always goes to the (0,0,0) point of the Plain(The ground). I checked ray hit point and bullet target position by debugging the bullet targetPosition and hit point position.

Debug.Log("hit  " + hit.point );

Debug.Log("Target " + targetPosition);

Can anyone show me what I am doing wrong here? What should I change?

1
Did you check if by any chance ** speed** of bullet is 0, when you are using Object Pool?ZayedUpal
Speed is 500. Speed is okSajitha Rathnayake
Vector3 targetPosition become (0,0,0) don't know whySajitha Rathnayake

1 Answers

0
votes

I found the problem in the PoolManager script. The return statement of TryActive method should be change from

return PoolItems[ItemIndex].transform;

to

return transform.GetChild(i).transform;