Tell the distance of prefab clones when they stop moving

Hi

So when you throw the objects I want to get the distance between them, after they stop moving.

var projectile : Transform;
var projectile2 : Transform;
var projectile3 : Transform;

var object1 : GameObject;
var object2 : GameObject;
var object3 : GameObject;

var notMoving = false;
var notMoving2 = false;
var notMoving3 = false;



function Awake(){

}
 
function Update () {

    if (Input.GetButtonDown("Fire1")) {
 
    	var clone : Transform;
    	var clone2 : Transform;
    	var clone3 : Transform;
   
    	clone = Instantiate(projectile, transform.position, transform.rotation);
    	clone2 = Instantiate(projectile2, transform.position, transform.rotation);
   	 	clone3 = Instantiate(projectile2, transform.position, transform.rotation);
 
   	 	// Add force to the cloned object in the object's forward direction
   	 	clone.rigidbody.AddForce(clone.transform.forward * 500);
    	clone2.rigidbody.AddForce(clone.transform.forward * 450);
    	clone3.rigidbody.AddForce(clone.transform.forward * 400);
    	
    	var distance = Vector3.Magnitude(object1.transform.position - object2.transform.position);
			Debug.Log(distance);

	}
}

1 Answer

1

It’s easier to maintain if you throw them into an array and break it out into functions so you can yield. Here’s a conceptual way of solving it, you would have to adapt it to your needs. I’ve tried to keep it as semantic as I can in terms of variable- and function names. You would probably want an offset from where they spawn or even perhaps a sequence (not sure what your implementation of this is), but it’s way easier when being able to yield and loop through an array.

var projectiles : GameObject[]; // Prefabs of projectiles

private var projectileTransforms : Transform[];
private var projectileRigidbodies : Rigidbody[];
private var isShot : boolean = false;

private var thisTransform : Transform;

function Start () {
	// Cache all component (mobile-friendly)
	thisTransform = transform;
	projectileTransforms = new Transform[projectiles.Length];
	projectileRigidbodies = new Rigidbody[projectiles.Length];
	for (var i = 0; i<projectiles.Length; i++) {
		var clone : GameObject = Instantiate(projectiles*, Vector3.zero, Quaternion.identity);*

_ projectileTransforms = clone.transform;_
_ projectileRigidbodies = clone.rigidbody;
* }
}*_

function Update () {
* // Shoot if Fire1 and not previously has shot*
* if (Input.GetButtonDown(“Fire1”) && !isShot)*
* ShootProjectiles();*
}

function ShootProjectiles () {
* isShot = true;*
* var tmpForce : float = 500;*

* // Shoot out projectiles*
* for (var i = 0; i<projectiles.Length; i++) {*
_ projectileTransforms*.position = thisTransform.position;
projectileRigidbodies.AddForce(projectileTransforms.forwardtmpForce);

* tmpForce-=50;
}*_

* // Wait while they move*
* while (HasMovement(projectileRigidbodies))*
* yield;*

* // Check distance once they’ve stopped*
* CheckDistance(projectileTransforms);*
}

function HasMovement (objs : Rigidbody[]) : boolean {
* for (var obj : Rigidbody in objs)*
* if (obj.velocity!=Vector3.zero)*
* return true;*
* return false;*
}

function CheckDistance (objs : Transform[]) {
* for (var i = 0; i<objs.Length; i++)*
* for (var y = 0; y<objs.Length; y++)*
* if (i!=y)*
_ Debug.Log(Vector3.Distance(objs*.position, objs[y].position));
}*

Good luck!_

Thank you so much. This is what I need.

One problem they shoot as soon as I click play - sorry no idea how to fix it still try to wrap my head around your script Thanks

They are instantiated at the same position which leads to a collision. It's better to instantiate objects that you plan to reuse at the load of level (if that is what you intend), especially for mobile devices where instantiating alters the memory demand during runtime which often leads to hickups. To "pause" rigidbodies you can set them to kinematic. Add this to the last line of the for-loop in Start(): projectileRigidbodies*.isKinematic = true;* And before AddForce() in ShootProjectiles(): projectileRigidbodies*.isKinematic = false;*

Thanks that fixed that, but it is still returning zero as the distance. Thanks for the help.