- Home /
Move objects infinite based on distance and not an invoke
So I kind of hit a wall with my logic and I need some help trying to flesh out this idea.
Currently I have an invoke that gets an object from an object pool and basically spawns it every second and it moves automatically. What I found is that when you increase the speed of the object, the distance between the objects spawning is increased, which is not the desired result. What needs to happens it that the distance between the spawned objects will remain the same no matter the speed of the objects, it will always spawn 10 units away from the next.
Clearly this bit of code is just terrible and is not at all reusable and only works once through.
void Start() { obj1 = GetPooledObject(); // get an inactive object obj1.SetActive(true); //set that inactive object to active. This begins object movement }
void Update()
{
distance = Vector2.Distance(obj1.transform.position, transform.position); //calculate distance between this object and objectpool start position
if (distance > 10 && !secondObject)
{
MoveSecondObj();
}
}
private void MoveSecondObj()
{
var obj2 = GetPooledObject();
obj2.SetActive(true);
secondObject = true;
}
Answer by Kiwasi · Jul 16, 2014 at 08:30 AM
Essentially you are describing a wave. The nature of waves is that speed = frequency * wavelength. If you increase speed you have to decrease frequency to get the same wavelength. In other words call your spawn function more often.
Invoke repeating is not well suited for this, use a coroutine or a custom timer instead.
Answer by smallbit · Jul 16, 2014 at 08:33 AM
If you dont need to use the obj1 for anything more than spawning procedure, you can use it to store the reference of your last spawned object. Than In Update you check its distance to the spawner (Transform.position) if its bigger than 10, you spawn another object and put its reference to obj1. This is simple code that could do that ( I assume that GetPooledObject() returns Transform type. )
Transform obj1;
void Start()
{
SpawnNextObject();
}
void Update()
{
distance = Vector2.Distance(obj1.transform.position, transform.position); //calculate distance between this object and objectpool start position
if (distance > 10)
{
SpawnNextObject();
}
}
void SpawnNextObject()
{
obj1 = GetPooledObject(); // get an inactive object
obj1.SetActive(true); //set that inactive object to active. This begins object movement
}
Calculating distance each frame is expensive. This answer could be more efficient if you use Vector3.sqr$$anonymous$$agnitude ins$$anonymous$$d.
Thanks guys, this really helped.
Also, how do I find out what is expensive and what isn't expensive?
So now there is a problem of inconsistent spacing of when the object spawns. I think this is a problem with inconsistent fps, therefore the distance can be inconsistent to the point where it is very noticeable to the player.
How would I go about fixing this problem?
How inconsistent is your FPS?
You could always adjust your spawn position exactly to suit based on the actual distance. But fixing your fps issues would be a better option.
The docs normally indicate weather something is expensive or not, or if there is a cheaper way. As a general rule anything with square roots should be avoided if possible.
So, it may or may not be an fps issue because it is always 84+, but what I think it might be is that Vector2.Sqr$$anonymous$$agnitude is executing the SpawnWall() too soon and I am unsure why. It is very erratic.
For it to start, I call SpawnWall() from another script and by doing that, it will set the rest of the Spawning script into motion.
void Update ()
{
if (spawnedWall)
{
distance = Vector2.Sqr$$anonymous$$agnitude(spawnedWall.transform.position - transform.position);
if (distance >= wallSpacing*wallSpacing)
{
SpawnWall();
}
}
}
public void SpawnWall()
{
spawnedWall = GetPooledObject();
spawnedWall.transform.position = transform.position;
spawnedWall.SetActive(true);
}
Your answer
Follow this Question
Related Questions
2D one distance at a time movement 2 Answers
Uneven speed in 2d movement script 2 Answers
Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers
Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer
How do I prevent characters from being able to land on each others heads? 1 Answer