- Home /
Game laggs when player comes into contact with enemy.
When the player comes into collision with the enemy it will have this god awful horrible lagg for some unknown reason it could be because of my script. It could be because of the collision itself, but probably my script. I'll attach the script down below.
function Update() {
if(Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,hit, 100.0)) {
if(hit.collider != null) {
attack(hit.collider.name,hit.collider.tag);
}
}
}
if(Input.GetKeyDown("escape")) {
Screen.lockCursor = false;
}
}
function attack(enemy,tag) {
if (tag == "enemy") {
pLife = player.GetComponent(playerStats).pLife;
pStamina = player.GetComponent(playerStats).pStamina;
pMeleeSkill = player.GetComponent(playerStats).pMeleeSkill;
pGunSkill = player.GetComponent(playerStats).pGunSkill;
pCraftingSkill = player.GetComponent(playerStats).pCraftingSkill;
pHead = player.GetComponent(playerStats).pHead;
pLeftHand = player.GetComponent(playerStats).pLeftHand;
pRightHand = player.GetComponent(playerStats).pRightHand;
pChest = player.GetComponent(playerStats).pChest;
pLeftHandWeapon = player.GetComponent(playerStats).pLeftHandWeapon;
pRightHandWeapon = player.GetComponent(playerStats).pRightHandWeapon;
//var
var damage: float;
if (pRightHandWeapon != "none" && pLeftHandWeapon == "none") {
//damage = (*(WeaponDamage*1.75));
}
else if (pRightHandWeapon != "none" && pLeftHandWeapon != "none") {
//damage = ;
}
else {
damage = ((pMeleeSkill*5)+7.25)*0.85;
}
GameObject.Find(enemy).GetComponent(zStats).eLife -= damage;
Debug.Log(GameObject.Find(enemy).GetComponent(zStats).eLife);
}
}
I use a mesh collider. Could it be that as the issue?
Answer by perchik · Aug 29, 2013 at 06:45 PM
It's probably all your GetComponent calls, which you could should just use once:
playerStats pStats = player.GetComponent<playerStats>();
pLife = pStats.pLife;
pStamina = pStats.pStamina;
pMeleeSkill = pStats.pMeleeSkill;
//etc
Ok thanks I will change that but no... I disable those scripts and it still had it. Beside that should only be active once I click on the target not when I come into contact. Like run into it.
All I'm saying is that your attack function is horribly slow with all the getComponent calls
Yes, it's fixed but that isn't the error which is causing it.
It's still slow. When I take off the meh collider it runs fine. So that the issue.
Your answer
Follow this Question
Related Questions
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
How to access a colliders function or variable 2 Answers
Multiple OnTriggerEnter on One Collider 1 Answer
ignoring collisions with a parent object 2 Answers