Try as I might, I absolutely cannot get physics.ignorecollision to work on my player prefab.
It used to work, then I added a kinematic rigidbody and capsule collider set as a trigger onto my prefab in addition to the capsule collider that exists on my character controller.
The weirdest part, is that I inactivated the child, and the problem still persists.
I cannot simply add the player to a layer to ignore, because all characters are instantiated on the server as players, so i cannot switch their layers because all characters could be shooting at the same time and the server is authoritative… any clues?
Player:
void Update()
{
// Play the shoot animation
if (Screen.lockCursor && !isReloading && Input.GetButtonDown("Fire1") && platformer.aimTarget != null)
{
ReloadBegin();
controller.Throw();
Invoke("SpawnSnowball", 0.1f);
Vector3 handpos = transform.position + transform.rotation * handOffset;
Vector3 aimDir = platformer.aimTarget.position - handpos;
networkView.RPC("Throw", uLink.RPCMode.Server, aimDir);
}
}
void SpawnSnowball()
{
// TODO: add time to live to snowball
Vector3 handpos = transform.position + transform.rotation * handOffset;
Vector3 aimDir = platformer.aimTarget.position - handpos;
GameObject newball = Instantiate(snowball, handpos, Quaternion.LookRotation(aimDir)) as GameObject;
Physics.IgnoreCollision(collider, newball.collider, true);
//Debug.Log (newball.transform.collider.name);
//Debug.Log (platformer.collider.name);
//newball.SetActive(true);
}
Snowball:
void Awake()
{
rigidbody.velocity = transform.forward * SPEED;
rigidbody.angularVelocity = new Vector3(SPIN, SPIN, SPIN);
Destroy(gameObject, timeToLive);
}
void OnCollisionEnter(Collision collisionInfo)
{
//Debug.Log (collisionInfo.transform.name);
//Debug.Log (transform.collider.name);
if (hitSplatter != null)
Instantiate(hitSplatter, transform.position, Quaternion.identity);
if (hitPlayerSplatter != null && collisionInfo.transform.root.tag == "Player")
Instantiate(hitPlayerSplatter, transform.position, Quaternion.identity);
Destroy(gameObject);
}
After extensive debugging, I have found that the code causing this error is the following:
//smoothly transition between initial height and crouch height
float adjustedHeight = Mathf.Lerp(character.height, newHeight, 5 * Time.deltaTime);
character.height = adjustedHeight;
//adjust center to match new height in order to keep base of collider in the same position without changing the characters position
Vector3 newCenter = new Vector3(0,adjustedHeight/2,0);
character.center = newCenter;
Should you ever find yourself in this situation, the question you need to ask, and the one that I will be asking in a new thread, is how do you get Physics.IgnoreCollisions to work properly with a character controller that has it’s capsule collider dynamically resized during Update(), since resizing it obviously destroys and reinstantiates it at the low level in unity, thus clearing it’s IgnoreCollision flags.
Here is the link to my thread on the forums discussing this in further detail, along with a proposed Unity feature request for a solution: http://forum.unity3d.com/threads/physics-ignorecollision-bug-feature-request.251226/