Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matthew012 · Aug 29, 2013 at 06:42 PM · javascriptcollider

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matthew012 · Aug 29, 2013 at 06:58 PM 0
Share

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.

avatar image perchik · Aug 29, 2013 at 07:27 PM 0
Share

All I'm saying is that your attack function is horribly slow with all the getComponent calls

avatar image Matthew012 · Aug 29, 2013 at 07:37 PM 0
Share

Yes, it's fixed but that isn't the error which is causing it.

avatar image perchik · Aug 29, 2013 at 07:38 PM 0
Share

So it's still slow or you're getting an error now?

avatar image Matthew012 · Aug 29, 2013 at 07:42 PM 0
Share

It's still slow. When I take off the meh collider it runs fine. So that the issue.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges