- Home /
Increase the speed but keep the spacing between the objects
I have been wondering something when I was playing around with an old script of mine
public float movespeed;
private Vector3 Direction;
void Start(){
movespeed = 4f;
Direction = Vector3.right;
}
// Update is called once per frame
void Update () {
transform.Translate (Direction * movespeed * Time.deltaTime);
This script lets an object, were it is attached to, move automatically to the right with a given speed. This script is attached to 1 object that gets instantiated every 1float by using the InvokeRepeat method. I was wondering why the spacing between 2 instantiated objects are greater when you increase the movespeed, as example from 4f to 5f.
I saw that if you increase the amount the function would be called that the spacing between the two objects would be smaller. Knowing this I used an exponential growth formula. Looking for 2 points were the spacing between objects are 4x.
Formula -> Float = B*A^t
Movespeed 4 equals a "t" of zero, 0. (movespeed 5 equals "t" of 1, movespeed 6 equals "t" of 2, etc.)
at movespeed 4 a float of 1 was given
at movespeed 10 a float of 0.4 was given
(0.4/1)^1/6 = A
0.4/A^6 = B
But even this won't give me a correct float for every movespeed. Even when calculating with 9 numbers behind the decimals. The spacing between the objects is off by [0.1x - 0.3x]
I was wondering if there is another method to increase the speed but keeping the spacing between the objects the same.
Answer by Bunny83 · May 24, 2015 at 11:58 PM
Uhm, isn't that obvious o.O?
If your object moves 4 units per second and you instantiate a new object at the same starting point every second, the space between them is 4 units as the first object has moved 4 units in one second...
If you change the speed to 5 units per second the spacing is of course 5 units.
I'm not sure how you come to the conclusion that any kind of exponential growth happens here...
edit
So you actually want to calculate at which interval you have to instantiate your objects to have them spaced with a given distance.
That's quite easy:
float CalulateInterval(float aSpeed, float aSpacing)
{
return aSpacing / aSpeed;
}
By using this method you simply pass your speed and your desired spacing between the objects and it returns the interval in seconds.
Some examples:
Speed spacing interval
------------------------
4 4 1
5 4 0.8
10 4 0.4
4 2 0.5
Oh, yeah I thought I could calculate the exactly float needed in the InvokeRepeat method to get a spacing of 4x everytime. that is what I meant. And it kinda works but as I said with a differential of [0.1x - 0.3x]
But yes your comment sounds logical. thx
Uhm, your question was "why is that" and not "How can i make the spacing stays the same even when i increase the speed"...
If you ask a question make sure you actually know what you want to ask. It's usually the best to write the title after you finished with the description of your problem...
I'll edit my answer...
You can say I put 2 questions in my post. (Title changed)
EDIT Sigh.. yes thanks for opening my eyes, my brain just derped really badly. I wonder how I did not saw that earlier. (Smashing head against table..)
btw: If you transfer this to real world units, just look at the unit equations:
spacing in meters ( m )
speed in meters per second ( m/s )
interval in seconds ( s )
spacing / speed == interval
m / m/s == s
If you would reverse spacing and speed you would get the frequency in Hz:
freq == speed / spacing
1/s == m/s / m
So the frequency tells you how many objects you have to create in one second while the interval tells you how many second you have to wait between each instantiation.
Yes, I have studied $$anonymous$$ath in University ._. I am not dumb xD..
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player rapidly accelerating instead of stopping 1 Answer
Projectile motion arc 2d 1 Answer
Math exponential number conversion 1 Answer