- Home /
Setting rigidbody.velocity of all child objects to "x".
I got a script where I spawn mountains for a background. These mountains go to the left, and have a velocity of "x".
I use a mountain spawner script. I instantiate the mountains, make them child of the spawner, and give them a certain velocity. Sometimes I will disable (SetActive(false)) the spawner. At this moment everything is okay, however when I re-enable it, all mountains that were previously placed have now a velocity of 0.
Unless there is a way to bypass this behaviour above, I would like to know how I can set the velocity of all mountains previously spawned to "x".
I tried using the following script:
void OnEnable()
{
Rigidbody2D m[] = GetComponentsInChildren<Rigidbody2D>();
foreach (Rigidbody2D mnt in m)
{
m.velocity = new Vector3(-1 * mountainSpeed, 0, 0);
}
}
As you may have guessed, it just doesn't work at all. It gives me an error:
Error CS0650 Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
I do not know how I should do this. When I instantiate the mountains, I do not keep track of them (at least I think not"), and just place them as a child of a spawner.
If you know the solution/answer, and it involves some "complex stuff", I will be glad if you explain what you did, so I will not have to ask this again :D
Thanks for the attention!!!
Note that in 2D you use Vector2 and not Vector3. Unity will implicitly convert to Vector2 for you but there's simply no need to create Vector3 like this.
Answer by MelvMay · Oct 28, 2015 at 01:35 PM
The error actually tells you what is wrong. To declare a managed array, the array rank specifier '[]' precedes the variable's identifier 'm' i.e. 'Rigidbody2D[] m =' and not 'Rigidbody2D m[] ='.
Alternately, just use 'var m = '.
https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx
Thanks, this worked! The full script I used is here, if anyone needs to use it, or an example. Thanks everyone else though, for the ideas! @$$anonymous$$elv$$anonymous$$ay http://pastebin.com/s80nevVi
Answer by Statement · Oct 28, 2015 at 01:41 PM
Rigidbody2D m[] = ...
Should be
Rigidbody2D[] m = ...
And then your loop looks stange.
m.velocity = ...
Should be
mnt.velocity = ...
Thanks! It worked. And true, the loop was wrong :P In case someone needs it, here is the script I used http://pastebin.com/s80nevVi @Statement
Answer by arcifus · Oct 28, 2015 at 05:49 PM
I would have the children handle their own velocity. That is each mountain can have a Mountain script attached and can refer to the speed setting in its parent.
// In the mountain script
Vector3 fixedVelocity = Vector3.zero;
Rigidbody rigidbody = null;
void Awake() {
fixedVelocity = Spawner.fixedVelocity;
rigidbody = gameObject.GetComponent<Rigidbody>();
}
void OnEnable() {
rigidbody.velocity = fixedVelocity;
}
// In your spawner script
static Vector3 fixedVelocity;
const float mountainSpeed = 10f;
void Awake() {
Spawner.fixedVelocity = Vector3.left * mountainSpeed;
}
Isn't it more expensive to do such? I mean you would use a script, etc. @arcifus
I was able to do it using my previous idea. http://pastebin.com/s80nevVi Still, do you think the logic I am using in the script above (the one I just pasted) is more efficient than your idea, or not? How much would I win in performance? @arcifus
I've tweaked the script with performance in $$anonymous$$d. Lets see what the changes imply:
The awake function for each mountain will only be called once during its life time
You will call GetComponent only once for each mountain
You will calculate the intended velocity only once in your Spawner script in its Awake
The only thing that will happen for each enable disable cycle is a direct assignment of a precalculated fixed velocity
If you are using the mountains for a scrolling background, make sure you only instantiate the number of mountains that are required to fill the screen view. After that, reuse existing objects by moving them into the starting position again. Instantiating objects is an expensive operation.
I've written these points above to guide you as to how you should think about performance when coding. Also, always run comparison tests of different approaches by generating a lot of load and watching the fps.
Good luck!
Vector3 fixedVelocity = Spawner.fixedVelocity;
You are just setting a local variable which will be discarded. Set the field ins$$anonymous$$d.
fixedVelocity = Spawner.fixedVelocity;
So I should not instantiate the mountain, and should give its velocity inside the mountain script, then use $$anonymous$$athf to make when it walks "x" distance, put it back to its original position?