I already do a shoot script
But how can I make Bullet go to limited distance start from player and then destroy
Have you considered timed destroy?
Destroy(gameObject, lifeTime);
It destroys is after certain period of time.
If you want to do it at certain distance, create a variable holding the position you fired the bullet.
Then add an if statement like this :
If(Vector3.Distance(currentPoint,startingPoint) > maxDistance) {
Destroy(gameObject,0);
}
thanks for replay
i try to do the script in bullet prefab but it doesn’t work
if u can help me with this
function Update () {
var mainplayer = GameObject.Find("CharacterPlayer"); //to get the player object
var dist = Vector3.Distance(mainplayer.transform.position, transform.position);
if (dist >= 100.0)
DestroyImmediate (this,true);
}
i put it to bullet prefab but it do nothing
DestroyImmediate (this,true);
this code destroy the script component not parent object
so how can i get the parent object from its component
well “this” points to the script, you should use gameObject for destroying the bullet itself.
Plus avoid using DestroyImmediate unless you have a precise objective. Use Destroy instead.