- Home /
mass spawning rigid bodies but only some keep the given velocity
So I have spent way longer than I would have liked on this and I still can't figure it out.
I am spawning anywhere between 1-50 simple enemy units that have a rigid body and in Start() I assign a velocity to them. Only some of the rigid bodies retain the velocity that I give them when the game is running. Roughly seems like 1/3rd of them lose the velocity. I would appreciate it if someone could shed some light unto this issue.
It is possible to solve this issue by checking in Update() but I feel like that is just redundant at this point and shouldn't be needed if I am doing this properly.
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float speed = 5f;
public Vector2 travelDir;
void Start(){
assignVelocity();
}
void Update(){
Debug.Log(rigidbody2D.velocity);
}
private void assignVelocity(){
travelDir = Random.insideUnitCircle;
rigidbody2D.velocity = Vector2.one;
rigidbody2D.AddForce(travelSpeed*speed);
}
}
Are the created objects effected by gravity? That would cause objects to shift their velocity. Also if any of the rigidbodies have drag, the velocity will decrease over time.
Are you noticing a sudden drop in velocity or does the velocity of the objects decrease over time?
Sorry I should have clarified a bit more. I have gravity turned off, all drag is set to zero. The issue is that only some of the objects actually have their velocity changed from the Start() method. It seemed like it wasn't being run every time. Can this happen? If so why would it happen?
Are you spawning them all in the same spot? If they are in close enough proximity to touch eachother they could be reacting to collisions and removing their initial velocity.
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
How to simulate AddForce? 1 Answer
Velocity and Time.scaleTime 2 Answers
What is the velocity on a given axis of a rigidbody? 2 Answers
How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer