So I have a weapon that ejects shells, and to do so it instantiates a shell prefab and sets it to the position and rotation of a part of the gun. This all works great most of the time, and It doesn’t seem to throw any errors on the iphone, but in Unity I get the following sometimes.
transform.rotation of 'shellPrefab(Clone)'is no longer valid. Input rotation is { -0.068610, 0.072949, 0.009855, -0.994925 }.
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) (at /Users/build/builds/unity-iphone-1.5/iphone-1.1/Runtime/Export/Generated/BaseClass.cs:62)
Weapon:Fire() (at Assets/Plugins/Weapon.cs:61)
<>c__CompilerGenerated0:MoveNext() (at Assets/GUI Scripts/Button4.cs:40)
This is the code I have for the shell, I tried a a try catch but that didn’t help.
try
{
Transform shell =(Transform)Instantiate(shellPrefab,shellEjectLocation.position,shellEjectLocation.rotation);
shell.animation.Play();
}
catch
{
}
shellEjectLocation is just the position on the gun where the shell should spawn. Any ideas why It only sometimes throws this error when I fire lots bullets?
Is this definitely the only place where the bullet’s rotation is set?
Yep, nothing else has anything to do with the shell. Might it have something to do with the fact that my Weapon Fire() method is called by a coroutine? I’ve pasted the coroutine(FireButton) and the method(Fire) below.
public IEnumerator FireButton()
{
isShooting = true;
while(isShooting)
{
Player.weapons[Player.currentWeapon].Fire();
if(Player.weapons[Player.currentWeapon].ammo>0)
Player.singleton.transform.animation.Play();
yield return new WaitForSeconds(Player.weapons[Player.currentWeapon].fireRate);
}
}
public bool Fire() //Returns True If Hit Enemy
{
if(ammo<1)
{
Reload();
return false;
}
if(isReloading)
return false;
ammo--;
StartCoroutine(MuzzleFlash());
if(audio)
SoundManager.PlaySoundEffect(audio);
try
{
Transform shell = (Transform)Instantiate(shellPrefab,shellEjectLocation.position,shellEjectLocation.rotation);
shell.animation.Play();
}
catch
{
}
RaycastHit hit;
Vector3 newRaycast = rayCastFrom.position;
newRaycast.y += Random.Range(accuracy*-1, accuracy);
newRaycast.x += Random.Range(accuracy*-1, accuracy);
if(Physics.Raycast(newRaycast, rayCastFrom.forward, out hit, 1000))
{
if(hit.collider.tag=="Headshot")
{
Debug.Log("Headshot");
TerroristZombie tz = (TerroristZombie)hit.transform.GetComponent("TerroristZombie");
if(tz)
StartCoroutine(tz.HeadShot());
return true;
}
else if(hit.collider.tag=="Enemy")
{
Debug.Log("Enemy");
TerroristZombie tz = (TerroristZombie)hit.collider.transform.parent.GetComponent("TerroristZombie");
if(tz)
tz.Shot(damage);
else
Debug.Log("Couldn't get component");
return true;
}
}
Debug.DrawRay(rayCastFrom.position, rayCastFrom.forward* 1000, Color.green);
return false;
}
[/code]
Sorry to bump, but does no one really have any idea why a simple instantiation only sometimes(1/200) fails???
Is the shell ejection position marker a fixed object or moving or an animated bone… ?
The shell eject position is just an empty gameObject. It is attached to the gun, and the gun is attached to an animated character. The gun moves with the player. However in testing it doesn’t seem to matter what the orientation of the gun is, or if its moving.