I moved this from Scripting to Support as Scripting may have been inappropriate…
This is destined for the iPhone and I am working in Unity iPhone, but this seems to be a general scripting issue rather than an iPhone issue.
The problem I am having is this:
I have a rigidbody player object. When the player activates their shields and hits the terrain mesh problems happen.
(Keep in mind that this is an imported mesh, not a Unity Terrain, as it’s destined for the iPhone.)
There are two problems, one related to the function ShieldsOn() and the other with the sound. They could be related.
I have been able to positively recreate the following behaviour:
If the shields go off while the player object is touching the ground, the player object becomes invulnerable as the function OnCollisionEnter() does not seem to notice that the boolean variable for the shield state has changed. This behavior persists until the shields are triggered again and the function finishes when the player object is flying rather than landed.
In more complicated terms, if the function ShieldsOn() finishes and, after the yield step, sets the variable shieldsOn to false while in contact with the terrain mesh and it’s colliders, then all subsequent collisions between the player object and the terrain mesh and colliders act as if shieldsOn = true. The only way to reset the behaviour of OnCollisionEnter() is to re-Trigger the function ShieldsOn() and make sure the player object is not contacting the terrain mesh when var shieldsOn is set to false.
I would have assumed that OnCollisionEnter() would check the variable shieldsOn : boolean; each time there was a collision. That does not seem to be the case.
The second clue is the sound effects… Tho’ I’m not sure what it’s telling me.
Let’s begin with the fact that all of these sounds have been imported as “uncompressed” and “force mono”. This is primarily because of the limitations of the iPhone. They are all sourced from .mp3 files in the finder before the Unity import.
The behaviour I’m getting with the sound is that when the shield sound is called and played in function ShieldsOn(), they play properly until impact with the terrain. The function **OnCollisionEnter()**plays the collision sound properly, but the shield sound stops playing. All of my other sounds play fine concurrently - engines, jets, etc. - but the upon OnCollisionEnter() shieldSound.clip stops.
This whole audio thing could be a red herring, but it is related to the OnCollisionEnter() event (and frankly I’d like the shield sounds to continue playing until they expire…)
Any tho’ts? Suggestions? Slaps in the face or other buffoonery? I could easily be making a blazingly stupid error, but if so, I can’t see it…
// ignoring lots of other code in this script...
// here are the relevant bits of code:
// INITIAL VARIABLES
var armor : float;
var shieldStrength : int = 5;
var shield1 : GameObject;
var collisionModifier : float;
private var shieldsOn : boolean;
private var canShield : boolean;
private var playerStatus : PlayerStatus;
// CLASSES
class SoundEffects {
var clip : AudioClip;
var volume : float = 1.0;
var loop = false;
var pitchVariation : float = 0.0;
}
var shieldSound : SoundEffects;
// FUNCTIONS
function Awake () {
canShield = true;
shieldsOn = false;
}
function Start() {
playerStatus = GetComponent(PlayerStatus);
if (!playerStatus)
Debug.Log("No link to Player Status");
}
function ShieldsOn () {
if (canShield) {
canShield = false;
shieldsOn = true;
shield1.active = true;
if (shieldSound) {
audio.clip = shieldSound.clip;
audio.volume = shieldSound.volume;
audio.loop = shieldSound.loop;
audio.Play();
}
yield WaitForSeconds (shieldStrength); // Currently 5.0 seconds
shield1.active = false;
shieldsOn = false;
canShield = true;
}
}
function OnCollisionEnter (collision : Collision) {
// DAMAGE CALCULATIONS
var collisonStrength = (collisionModifier * collision.relativeVelocity.sqrMagnitude);
if (!shieldsOn) {
if (collisonStrength > armor)
playerStatus.currentHealth -= (collisonStrength);
}
// AUDIO
var collisionSoundEffect : CollisionSoundEffect = collision.gameObject.GetComponent (CollisionSoundEffect);
if (collisionSoundEffect) {
audio.clip = collisionSoundEffect.audioClip;
audio.volume = collisionSoundEffect.volumeModifier * collision.relativeVelocity.sqrMagnitude * specialEffects.collisionVolume;
audio.Play ();
announcementC01.textMessage = "OCE-shieldsOn: " + shieldsOn + "\nOCE-Can Shield? " + canShield; // For Debugging
}
}
// Behavior is: If after the yield, the ShieldsOn () function sets the variable shieldsOn = false
// when in contact with the terrain collider, on the next and subsequent impacts with that collider
// the OnCollisionEnter () function seems to fail and neither applies damage nor plays the collision sound.
// The only way to return the OnCollisonEnter behaviour to what is expected is to re-Trigger function
// ShieldsOn() and make sure it expires and sets the variable shieldsOn = flase when NOT in contact
// with the terrain collider.