Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

localPosition for weapon instantiation not correct when client views host

Discussion in 'Scripting' started by tomdiam94, Nov 25, 2019.

  1. tomdiam94

    tomdiam94

    Joined:
    Oct 12, 2019
    Posts:
    2
    When the player spawns in game, an EquipWeapon method is played at start, which first sets the weapon as the child of a weaponHolder object on the players right hand, then locally transforms the weapon on the weaponHolder. This works fine for the host. I can see that in the game and scene view it is correctly placed. When I spawn a client, the clients weapon also spawns correctly for the client in scene and game view. However, when the client walks up to the host, the hosts sword appears incorrectly (screenshots attached). The client game view of the host and the scene view are out of sync with regards to the sword position, which is the issue.

    I suspect it has something to do with the transform to local position and how EquipWeapon is called. Is there a way to adjust it so that it appears the same between client and host and client to host?

    Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. [System.Obsolete]
    7. public class WeaponManager : NetworkBehaviour
    8. {
    9.  
    10.     [SerializeField]
    11.     private Transform weaponHolder;
    12.  
    13.     [SerializeField]
    14.     private PlayerWeapon primaryWeapon;
    15.  
    16.     private PlayerWeapon currentWeapon;
    17.  
    18.     private GameObject weaponIns;
    19.  
    20.     void Start()
    21.     {
    22.         EquipWeapon(primaryWeapon);
    23.     }
    24.  
    25.     public PlayerWeapon GetCurrentWeapon()
    26.     {
    27.         return currentWeapon;
    28.     }
    29.  
    30.     void EquipWeapon (PlayerWeapon weapon)
    31.     {
    32.         currentWeapon = weapon;
    33.         weaponIns = (GameObject)Instantiate(weapon.graphics, weaponHolder.position, Quaternion.Euler(weapon.rotation), weaponHolder);
    34.         weaponIns.transform.localPosition = weapon.position;
    35.        
    36.     }
    37.  
    38.  
    39. }
    40.  
    Weapon class (just in case):


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [System.Serializable]
    7. public class PlayerWeapon
    8. {
    9.     public string name = "Longsword";
    10.     public int damage = 10;
    11.     public float range = 0.8f;
    12.     public GameObject graphics;
    13.     public Vector3 position = new Vector3(-.07f, 2.329f, 1.281f);
    14.     public Vector3 rotation = new Vector3(0f, 90f, 22.221f);
    15. }
    16.  
    17.  
    18.  


    HostViewOfClient.PNG ClientViewOfHost.PNG
     
  2. tomdiam94

    tomdiam94

    Joined:
    Oct 12, 2019
    Posts:
    2
    Well, it appears shortly after posting I was able to solve it by first using the parent rotation and setting local rotation after. So, all is well.