Bullet hits Trigger to Load Next Level?

Hello, I am trying to write a script in which a bullet (prefab) triggers the next level when you shoot the triggerbox.

Currently, the next level does not load when I shoot it but it does load when I walk into it however. Any help would be greatly appreciated! I also have no attachment to these scripts, if someone has a better one from scratch that is absolutely fine!

I have no tags on either the Trigger or the Bullet, they are Untagged and Default.

Here is the script that I have placed on my weapon that spawns the bullet on left click.

And here is the script I have for the Trigger that is supposed to load the next level after being shot.

Rather than publishing images of your code, please past the text of the script into the question and then use the 101/010 button to format the code.

1 Answer

1

Try slowing down your bullet a bunch. That way you can be sure it isn’t doing strange things due to speed (which is very common).

Might I suggest using rays? You could shoot out a ray and whatever object it hits send it a message. Then all you do is make sure your button object has (for the example below) a loadNextLevel() method.

// this is c#

// initialize the ray data
RaycastHit theHit = new RaycastHit();
// get the two vectors for the ray
Vector3 fireDirection = gameObject.transform.forward;
Vector3 position = gameObject.transform.position;

// if it hit something within given range
// starts from position and fires toward fireDirection
if(Physics.Raycast(position, fireDirection, out theHit, range)) {
	// tells the hit object to execute a loadNextLevel() method
	// DontRequireReceiver means it won't give you error messages if the hit object doesn't have loadNextLevel() as a method
	theHit.collider.gameObject.SendMessage("loadNextLevel", SendMessageOptions.DontRequireReceiver);
}