Bullet prefab doesn't go to given target
I am developing my first FPS game. Ill try to explain what I have done so far and what I did try to solve my problem. May be I am missing single point. So please show me what that is :D. This will be long question but please read to end.
There is a FPS controller that controls the player and there is a weapon attach to player. weapon game object has child object called ButtetSpown. I used plain as a 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 give this point to Instantiated bullet
Instantiate process code here (I have removed gun animation codes here)
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 WORK FINE :) . bullet got its target correctly. Problem happens after I add PoolManager to control prefabs :( . PoolManager script written by myself. Here it is. PoolManager script attached to 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 instantiate prefabs will add as children of PoolManager gameobject.
Then I change my Gun script update function as bellow
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. Now Bullet active in SpownPoint correctly but the bullet does not go to actual target. it always go to (0,0,0) point of the Plain(The ground). I checkd ray hit point and bullet target position by Debugging. bullet targetPosition also have hit point position.
Debug.Log("hit " + hit.point );
Debug.Log("Target " + targetPosition);
Can anyone show me if i am doing wrong here? Where should it change?
Your answer
Follow this Question
Related Questions
Getting an Exception on Endless Runner 3D game 1 Answer
error CS0117: `System.Console' does not contain a definition for `ReadKey' 0 Answers
GameObject fields evaluate to null, but they are actually assigned and working 1 Answer
Movement Unit (RTS) 0 Answers
Car from Unity's standard assets giving me of null reference errors 0 Answers