obvious logic error on my part somehow

void Update () {
		roll=Random.Range(1,100);
		if((numBullets>0  roll<=percentFire)  (lastFire<Time.frameCount+fireDelay))
		{
			// Debug.Log("Alien : " + gameObject.GetInstanceID() + " Fired");
			numBullets-=1;
			lastFire=Time.frameCount;	
			fireBullet();		
		}
		if(numBullets<=0)
			numBullets=3;		
	}

Here I thought that each alien would have a maximum of 3 shots since my setup is like:

public int lastHit=0;
	public int numBullets=3;
	public int percentFire=20;
	public int fireDelay=80;
	public int lastFire=Time.frameCount;
	public int roll=0;	
	GameObject missile;
	Vector3 pPos;
	public GameObject objBullet;

BUT the result is kind of… sorry… player is not mean to live in my game… :slight_smile:

37018--1382--$picture_1_202.png

This?

      if(numBullets<=0)
         numBullets=3;

You immediate reset the number of bullets to 3, so your alien can keep firing. You might want to insert a delay for the reload.

But it doesn’t explain the no delay.
I thought about that and redid my logic some, and the alien fires no matter what:
New logic:

	// update per frame
	void Update () {
		roll=Random.Range(1,100);
		Debug.Log("Bullets: " + numBullets + "  Roll: " + roll + "  percentFire: " + percentFire + "  lastFire: " + lastFire + "  Time.FrameCount: " + Time.frameCount + "   fireDelay: " + fireDelay);
		if(numBullets>0)
		{
			if(roll < percentFire)
			{
				if(lastFire<Time.frameCount+fireDelay)
				{
					// Debug.Log("Alien : " + gameObject.GetInstanceID() + " Fired");
					numBullets-=1;
					lastFire=Time.frameCount;	
					fireBullet();		
				}
			}
		}
		
		//if(numBullets<=0)
		//	numBullets=3;		
	}

There SHOULD be a delay from what I have in the code, from the debug:

Bullets: 3
Roll: 90
percentFire: 5
lastFire: 7
Time.FrameCount: 1
fireDelay: 1600

scratches head
According to my logic, I should have never gotten past the
if(roll < percentFire)

since that is false…

edit:
also, the alien fires until he is out of ammo, so that logic atleast works

Try changing

 if(lastFire<Time.frameCount+fireDelay)

to

 if((lastFire+fireDelay)<Time.frameCount)

How you currenlty have it lastFire should always be less than frameCount, and definitly less than the current Frame count + the delay number. Add the delay to to the lastFire.

Thanks, I’ll give this a try tonight.
The other problem I have, is the collision ignore statement doesn’t always work, meaning that I have so many collisions going on at any given time, the engine can’t seem to handle it and some of the collision happen anyway. Its like the engine is on collision checking overload and can’t handle all of them.

I would imagine this would be an Ageia bug, definaly not a unity bug, since its part of the physics engine. I worked with the firing code for the invaders until 2am and gave up for the night and went to bed. There is definately some math that is wrong in my conditional statements, once thats straightened out, it should all work ok.

Also, because of the collision bug, I have to set 2 things in every game loop that I should not have to set. That is the Z axis on all shots since even though I am telling them on start to ignore collision with the invaders, the shots are deflecting off the invaders randomly thus changing there angle of decent, on top of that, the velocity for the rigid body gets ignored randomly.

As soon as I am finished with the aspects of the game that I am comfortable with, I’ll be posting the full project for anyone who wants to take a shot at figuring out all of these physics bugs and point out my coding flaws, can have at it.

I’ve had to reset the velocity in the game loop of each shot if the velocity is not = to the force that I desire.

I then had to create another 2 trigger boxes that removes the shots that go out of wack and are not corrected on there angles of decent. I have learned alot about collisions and triggers during this project, and for the sound, although I am sure this is documented, a single sound source can’t have more than 1 active sound or it stops the last sound and plays the new sound instead. I’ve also had to impliment some threading code in the project, so its got a little bit of everything in it at the moment.

I just noticed something, in my startup routine which only happens 1 time during the life of the game, I have:

public int lastFire=Time.frameCount;
In my example, this is returning 7 for the frameCount.

Now in my game loop, which happens at every frame, the Time.frameCount returned a 1. So, is there a mistype in the docs on what Time.frameCount really is doing? From what I understood, this is from when the game began, which included the startup time, but according to my report in the post, Time.frameCount is reset between the startup and the game loop. Any hint on this? Thanks

I doubt it.
There are two limitations with ignorecollision right now, but there is no such thing as overloading the physics engine.

  1. If you deactivate and activate a collider again it will lose its ignore collision state. Thus you need to reapply when deactivating a game object. (This is documented)
  2. The other limitation is that character controllers don’t support ignore collision in 1.6.2.

Of course there is always the off chance i am wrong, you can make a small sample project that shows the issue if you like.

Not doing 1 or 2.
Once I deactivate it, I deactivate it on both the invader to bullet collision check and the bullet to invader collision check (otherwise one registers while the other doesn’t and I don’t want it to register on either side)

I personally think the collision has already occured on spawn of the object before the startup conditions happen, so the collisions exist after the object exists and before its script triggers. Atleast that is my observation.

Not sure if I totally understoon 2, but neither of these are based on the character. This is just 2 rigid bodies with an attempt to ignore collision between the two, don’t have the code here with me (wife denied me my purchase of the mac book pro, very sad for me), however, knowing what it is doing and how I have created the objects, I should be able to whip something up tonight that I can throw up here to look at project wise.

Damn, and some people were complaining about my version being too hard! I hope you’ve implemented a “I surrender!!!” button, 'cause I’m hitting that right off. :wink:

Instead of implementing the delay manually, I think it would be better to do something like

var fireDelay = .8; // delay in seconds

function Start() {
	InvokeRepeating("Fire", 0, fireDelay)
}

function Fire() {
	//Firing routine here
}

(Sorry for the Javascript.) You probably don’t want to use frameCount, because then the speed of firing would depend on the framerate.

–Eric

Good point, right now I am working on a quick script for my collision ignore issue, then I’ll look at your solution for the fire rate.

Fast coding that demonstrates the issue.
Check out the debug log.
Obviously I have done something wrong, just don’t know what.

37146–1387–$collisionexample_207.zip (225 KB)

How you have it now, the order of the functions called are (I added sart and awake):

  1. spawnGun()
  2. Awake() in Bullet script
  3. spawnBullet()
  4. Awake() in Gun script
  5. Collision with bullet in Gun script
  6. Collision with gun in Bullet script
  7. Start() in bullet script
  8. Start() in Gun script

If you call Physics.IgnoreCollision in SpawnBullet() right after you instantiate the bullet the collisions are ignored. When the call is placed there the call order is:

  1. spawnGun()
  2. Awake() in Bullet script
  3. spawnBullet()
  4. Awake() in Gun script
  5. The call the Physics.IgnoreCollisions
  6. Start() in Bullet script
  7. Start() in Gun script

Interestingly enough, placing the call to Physics.IgnoreCollision in either the Awake() or Start() functions the IgnoreCollisions does not take effect until after the collisions have occurred. It appears the call to IgnoreCollisions is deferred by some means.

The problem is, I need to ignore collision after both objects exist, this is a small 2 object example, I have 150 bullets and 50 invaders to do this with against 4 bunkers and 1 player. So thats a larger scale.

In your fireBullet() function are you instantiating the bullet? Also, are the bullets instantiated in scripts attached to each invader or in a single script. If in a single script, right after the Instantiate, call a rountine to go through the list of invaders and whatever objects and call IgnoreCollision() on each pair. If there ends up beingg a performance problem with the IgnoreCollision() then you could get fancy and calculate the objects that would need the IgnoreCollision() and only call on those.

If using the individual scripts attached to each Invader, then you could keep track of the Invaders, bunkers, etc. in a controller object that also had a method that accepted a passed object and would iterate through objects to ignore list and perform the IgnoreCollision().

On invader spawn, it houses 3 bullets. When it fires those bullets, it instansiates them as clones against a prefab of the invader bullet. This lives until it collides with either a bunker, the player, or the base of the screen. Likewise, the aliens are spawned against prefabs, so are the bunkers, even the player, all spawned via prefabs. So its not on game start, its as needed. So, the problem is, like the example, I need to detect the collider and ignore the collision, which in and of itself is getting ignored without an error.

I slept on this problem some last night, the only conclusion I could come up with was to have some sort of method placed into the unity engine that turns off all collision between rigid bodies unless asked to be turned on. Right now, all rigid bodies with any collision type based on the physics rules stated in the manual, happen from the word go.

I used the repeater in the same project, will upload a new copy of the project tonight with a call to the repeater to show something else I discovered. From the time the simulation begins to the time the scene renders, there is a spawn of 3 bullets in my repeater example. This means that those three collide when the scene is rendered and split away from each other, when in fact, only 1 should spawn when the scene first renders to view, then repeat ever .8 seconds after each other. Which is not the case.

This means, another method needs added to unity for the repeater and spawning, which is so that all objects are disabled until all preloading is complete and the actual game loop begins. I can easily demonstrate this problem and show the reason. That is without a doubt, a problem for arcade style games. I will definately upload a project tonight showing this issue.

Anyone have a solution to this bug or recommendation for the bug?
This is an internal unity issue based on the inability to disable the collision between objects after collision has been detected for movement purposes.

So, wait–you’re trying to call Physics.IgnoreCollision inside of an OnCollisionEnter event? And expecting that to work?

Can you upload an example with the problem with the repeater? It seems solvable to me, but I need to see how you are setting up the repeater. I must be missing something in interpreting you explanation of the problem.