- Home /
instantiated objects have different speed
Hello,
I have a spawner object and I want to Instantiate an enemy at a given time. This enemy (a prefab) has a linear movement, with a specified speed, set in the inspector. The problem is that when the enemy spawns, sometimes his speed is higher than normal, or slower... Why is that?
Spawner script:
#pragma strict
InvokeRepeating("Spawn", 2, Random.Range(1,5)); //We spawn an enemy between 1 to 5 seconds
var dire : int; //The direction the enemy follows (left or right, set in the inspector)
var en : GameObject; //The enemy game object;
function Spawn () {
var enemyIn = Instantiate(en, transform.position, transform.rotation);
enemyIn.GetComponent(Enemy).dir = dire; //set the enemy direction
}
Enemy Script:
#pragma strict
var dir : int; //the direction of the enemy, set by the spawner script
var speed : float; //the speed of the enemy, set on the inspector
function Start () {
rigidbody2D.velocity.x = dir * speed * Time.deltaTime;
yield WaitForSeconds(5);
Destroy(gameObject); //we destroy the gameobject for memory reasons
}
The enemy rigidbody is set to "Fixed Angle" and Gravity Scale = 0. Everything else is default.
You may be colliding with the the spawner object and this could effect it, does your spawner manager "script" have a collider attached to it?
No, the spawner is an empty game object. Also, if I pause the game and check the insantiated objects, they all have the same speed in the inspector, even if they are moving at different speeds. So I believe there is a physics problem involved
Answer by jmgek · Sep 11, 2015 at 10:04 PM
Okay so if that's not the issue I looked at your code and Time.deltaTime should be in an update function and not in the start function. Because you are assigning your objects velocity at the start you don't need to update the time and the time that you are multiplying is actually equal to the actual fractions of seconds it takes for the frame to calculate.
#pragma strict
var dir : int; //the direction of the enemy, set by the spawner script
var speed : float; //the speed of the enemy, set on the inspector
function Start () {
rigidbody2D.velocity.x = dir * speed;
yield WaitForSeconds(5);
Destroy(gameObject); //we destroy the gameobject for memory reasons
}
alternativly if you wanted you could use this:
function Update () {
// Move the object 10 meters per second!
var translation : float = Time.deltaTime * 10;
transform.Translate (0, 0, translation);
}
I may be wrong so let me know if this worked.
Also, "we destroy the gameobject for memory reasons" is not entirely true. to destroy an object it takes more processing memory to rid the memory from the vram so in that case I would just pool the objects seeing how you may be concerned about memory or want best practices. Or deactivate the objects ins$$anonymous$$d of destroy, seeing how you "Could" have a large number of objects being destroyed at the same time, (one in a million case where you have 40 objects being destroyed on the same frame) this will drop your frames. https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
I would have regarded you the best answer if I could. $$anonymous$$y problem was pending from 2 days and this answer solved it. Thanks :)
Thank you jmgek for this!! I was having the exact same problem with a fresh install of Unity 2021!!!
Very helpful!!