- Home /
How to move and leave trail of instantiated objects behind in defined intervals between them ?
Hello,
I need to set a trail of objects behind player at certain intervals between them. Imagine it like a vehicle that lays mines behind or setting a poles for a fence. There are some conditions:
creating objects is triggered by pressing a button (pres button -> start laying objects, release button -> stop laying objects)
instantiated objects must be at a certain distance from one another
created "poles" stay on scene and player can turn around and add more "poles" to the fance - added poles must also be created at certain distance from existing ones.
This is my solution so far (I used OnTriggerStay method in cooperation with CompareTag):
public class PoleTrailInstantiation : MonoBehaviour
*//variables....*
public GameObject Pole; *// object that would be instantiated, contains a trigger set to certain radius that prevents object instantiation and is tagged "CreatedPole"*
public Transform polePosition;
public Transform CreatorObject; *//empty object inside of player where "poles" are instantiated*
public GameObject Player;
private bool insideTrigger = false; *//this will tell me if I left trigger and instantiation would be allowed*
// Update is called once per frame
void FixedUpdate() *// this works only in fixedUpdate*
{
if ((Input.GetKey(KeyCode.Space) && isInsideTrigger == false))
{
InstantiatePole(); *// function that creates objects - "poles"*
}
insideTrigger = false;
} //end of FixedUpdate
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("CreatedPole")) *//compares if trigger player stays in belongs to "createdPole" object)*
{
isInsideTrigger = true;
print("OnTriggerStay activated!");
}
}
And it actually works with one exeption. When player sets a trail of objects, stops creating them, turns around and starts creating a new trail, when players get near previously created "poles" there is a gap in instantiated objects cause trigger fields of both "poles" (created first and that are created now) overlap and constantly call OnTriggerStay method whis in my solution sets insideTrigger bool to true which prevents object instantiation :(
Any idea how can I get around that ? Perfection is so near but I just can't get to it.
To circumvent this logic problem, it would be feasible to ins$$anonymous$$d handle it without triggers (which is good for performance, too). For example, you could store the position of the player when he drops an object, and as long as his current position (after some movement) is below a certain threshold, not drop a new object. Once the threshold is reached, drop a new object, and store the current position again. This would prevent dropping any object near the last one only.
I considered that. I also considered a timed instantiation. But it won't prevent instantiation of new objects near aldery existing. It could cause some objects to be abnormally close to one another. If object is instantiated no other object should be instantiated in a radius around it.
But... this is already handled by your code, no? I fail to see the problem.
I implemented solution based on distance traveled from last instantiated object, combined with trigger that prevents object instantiation in close vicinity of previously instantiated objects. Unfortunately the gap still occurs in certain situations. I think it is unavoidable cause no matter what there will allways occur a situation where player stays inside trigger and is at certain distance from last instantiated object and also is inside trigger of previously instantiated object. Thank You for suggestion. It helped to trimm the problem down a bit (and i learned few new things to ! :) ).
Your answer

Follow this Question
Related Questions
Checking if object intersects? 1 Answer
setting variable in game object with instantiated object reference 2 Answers
FPS instantiate objects 1 Answer
Using Instance Nodes, worth it? 1 Answer