Search Unity

Weapon system concern

Discussion in 'Multiplayer' started by JonasrDJ, Aug 9, 2014.

  1. JonasrDJ

    JonasrDJ

    Joined:
    Jul 15, 2012
    Posts:
    8
    Hi there. I have a weapon system that works like so.

    Code (JavaScript):
    1. var weaponEquipped : boolean;
    2.  
    3. function OnTriggerEnter(other: Collider){
    4.     if (other.tag == "Player"){
    5.     weaponEquipped = true;
    6.     HPAmmo.Ammo += 10;
    7.    
    8.     }
    9.     }
    10.  
    11. function Update () {
    12. if(weaponEquipped == true) {
    13. this.transform.position = GameObject.FindGameObjectWithTag("LeftArm").transform.position;
    14. this.transform.rotation = GameObject.FindGameObjectWithTag("LeftArm").transform.rotation;
    15. collider.enabled = false;
    16. }
    17. }
    This works as it should, locally but when I fire this up online, the weapon seems to lagg a lot & the position update is slightly slower than the movement of the player. What is an alternative solution to a weapon pickup system. Thank you.
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    You should find the LeftArm gameobject in the script start or awake function.... calling gameobject.find in update over and over again will cause a bit of a slowdown.
     
  3. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Exactly. That's a really expensive update call. There are a few different ways of doing this, but I believe they all involve parenting. I'm going to assume this script is a component of the weapon, in which case you should just parent the weapon to the left arm and let unity handle the positioning. I'm actually about to do this in a game I'm writing as well, but have done it in the past for other projects.
     
  4. JonasrDJ

    JonasrDJ

    Joined:
    Jul 15, 2012
    Posts:
    8
    I fixed the occuring lagg by doing this! Thank you. My other problem right now, is that my movement script is causing the weapon to kinda fly around. I'm transforming the player when certain buttons are pressed & so does the arm obviously. This makes the weapon kinda fly out of place whenever I move. How can I fix that.
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    jtsmith1287's solution should work with this, make the weapon a child of the left arm gameobject when it is picked up.... then it will move wherever the arm moves