- Home /
Instanciate with parameter
I have a bullet prefab which I instantiate whenever the gun fires. The Bullet-Prefab has a script, which moves the bullet, and also a timer, which dictates for how long the bullet should travel. I do a raycast and pass the float how long the bullet should move to the gameobject. But the problem is, that The time it takes from instantiating the bullet, to bullet.GetComponent().startCount(time);
is long, and the result is the bullet penetrating the wall. I would like to know if there is a way to instantiate the prefab with a parameter (in this case, the time) so there is no time delay between the instantiation and the passing the parameter.
This is how I move the bullet:
using UnityEngine;
using System.Collections;
public class bullet : MonoBehaviour {
public float speed = 80;
private bool stop = false;
// Update is called once per frame
void Update () {
if (!stop)
transform.Translate (Vector3.forward * speed * Time.deltaTime);
}
public void startCount(float lifeTime){
StartCoroutine(count(lifeTime));
}
IEnumerator count(float lifeTime){
yield return new WaitForSeconds(lifeTime);
stop = true;
}
}
You have the wrong problem. You are doing something wrong with your time calculation, or misunderstanding the nature of discreet physics. Their may be a fence post error, or you are simply demanding too much of the physics engine. Either way this is not the problem.
Incidentally I've never seen this done before. Why not simply have the bullet keep flying until it enters a collision with the wall? Or am I missing the intricacies of this problem.
Im agree with Bored$$anonymous$$ormon.
Also
using UnityEngine;
using System.Collections;
public class bullet : $$anonymous$$onoBehaviour {
public GameObject urPrefab;
public NameOfPrefabScript urScript;
void Start () {
urScript = urPrefab.GetComponent(NameOfPrefabScript);
}
// Update is called once per frame
void Update () {
urScript.variableThatNeedToChange = "ur changes here";
Instantiate(urPrefab, transform.position, transform.rotation);
}
}
Answer by fafase · Feb 16, 2015 at 08:12 AM
I don't really get how the time between two lines can be too great since it is done in the same frame so the click counter does increase but you don't use that, while the frame time is the same for both method.
Now it is true that Instantiate is not the fastest, so you could reduce that overload wit reusing items, particularly for items like projectile that you reused a lot.
I would recommend a pool of object: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling .
This way you are not creating but just activating/deactivating items.
Ok I am going to look into that. Well I have set up a "slowmotion" mode, where I set the Time.timeScale to 0.1f. While activated, the bullet always stops on impact. But without the timescale, about 50% of the shots go through. This is why I suspect that the .getComponent is slowing things down.
Answer by Kiwasi · Feb 16, 2015 at 08:23 AM
I'd almost bet on this being a fence post error. Your bullet is moving one physics step too far before the timer expires. Try subtracting the value of Time.deltaTime from your time value before you pass it in.
I move the bullet inside the update function with transform.translate. I don't think that those are physics steps. However I am also having trouble fixing the bullethole to the surface of a moving physics object. There I am sure that the physics steps are messing things up, because my bullethole is sometimes fixed about one whole unit away from the actual object.... Any idea?
How do you actually check for collision? Because it could be a case of missed collision. Your bullet could be one frame entirely on one side and next frame entirely on the other side. Then no collision occurs. Solution, use linecast between previous and current position and see if it collides.
The bullet is not a rigedbody. It is just an empty gameobject. I do a raycast and deter$$anonymous$$ the "collision" by just setting the lifetime according to distance/speed.
Would have top post the code but I think it is a different issue then and would deserve its own post.
Your answer
