Nigey
February 15, 2014, 6:50pm
1
Hi Guys,
I’m using a Physics.OverlapSphere… It’s working okay in itself. I’ve Debug.Logged it and it’s recording a long array of collected objects… The issue is when I try to add force to the collected objects the while loop breaks and it doesn’t add any force. I think that means the game doesn’t recognising me adding force… Does anyone know what’s happening?
IEnumerator Shoot()
{
Debug.Log("STARTED");
while( timeElapsed < shotsToFire )
{
Debug.Log("CONTINUED");
Instantiate(pushPfx, transform.position, Quaternion.identity );
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100000);
int i = 0;
while (i < hitColliders.Length)
{
Debug.Log("HIT: " + i);
if( hitColliders[i] != this.gameObject);
{
Debug.Log("Meets Criteria");
hitColliders[i].gameObject.transform.rigidbody.AddForce( (transform.position - hitColliders[i].transform.position) * 10000, ForceMode.Impulse);//AddExplosionForce(pushForce, transform.position, 2000);
}
i++;
}
timeElapsed++;
yield return new WaitForSeconds(fireInterval);
}
Debug.Log("BLOODY ENDED");
}
Can someone help?
jister
February 15, 2014, 7:23pm
2
maybe obvious but… did you check if all the objects in hitColliders actually have a rigidbody?
also don’t have to go through gameObject.transform you can just go from collider to rigidbody like hitColliders*.rigidbody…*
Nigey
February 16, 2014, 12:20am
3
Okay I just tried that… Same issue. Here:
IEnumerator Shoot()
{
Debug.Log("STARTED");
while( timeElapsed < shotsToFire )
{
Debug.Log("CONTINUED");
Instantiate(pushPfx, transform.position, Quaternion.identity );
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100000);
int i = 0;
while (i < hitColliders.Length)
{
Debug.Log("HIT: " + i);
if( hitColliders[i] != this.gameObject hitColliders[i].GetComponent<Rigidbody>() != null);
{
Debug.Log("Meets Criteria");
hitColliders[i].gameObject.transform.rigidbody.AddForce( (transform.position - hitColliders[i].transform.position) * 10000, ForceMode.Impulse);//AddExplosionForce(pushForce, transform.position, 2000);
}
i++;
}
timeElapsed++;
yield return new WaitForSeconds(fireInterval);
}
jister
February 26, 2014, 2:51pm
4
did you try a for loop instead of the while?
If the force code is being run, I expect the problem is elsewhere.
Two obvious reasons come to mind:
You have something setting the velocity elsewhere
The rigidbody is kinematic