- Home /
Get Object ID upon collision.
What I want is for the player to have a separate object/objects for "Hitboxes" and when the enemy collides with them, to get the ID of the hitbox they collided with and to call its variables such as "Damage" "knockback" "Elemental Attribute" and stuff such as that. Also to use its ID to do its own "Am I getting hit by the same hitbox" check during its update phase, so that an enemy is invulnerable to getting hit by the same attack multiple frames in a row.
How would I go about getting the ID of the object they just collided with?
Comment
Answer by el_kloklo · Apr 02, 2014 at 05:14 PM
The exact request is :
hitlocal.collider.gameObject.GetInstanceID()
Here is some code I use similar to what you need
Vector3 trajectoryHitBox=HitBox-this.transform.position;
RaycastHit[] hitinfo;
hitinfo=
Physics.RaycastAll(this.transform.position,trajectoryHitBox,explosionRadius).OrderBy(h=>h.distance).ToArray();
Debug.DrawRay(this.transform.position,trajectoryHitBox,Color.red);
foreach(RaycastHit hitlocal in hitinfo)
{
Debug.Log(this.name+" explo check id colli "+hitlocal.collider.gameObject.name+hitlocal.collider.gameObject.GetInstanceID()+" victim "+Victim.gameObject.name+Victim.gameObject.GetInstanceID());
if(hitlocal.collider.gameObject.GetInstanceID()!=Victim.gameObject.GetInstanceID())
{
if(StaticInfoManager.Instance.IsShotBlocking(hitlocal.collider.gameObject))
{
hitpoint--;//in anticipation of the incrementation
break;
}
}
}
Best regards