Search Unity

Camera.GetComponent(Transform).rotation on multiplayer problem(Solved)

Discussion in 'Scripting' started by tijanikun, Nov 30, 2018.

  1. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Hello,
    I need help with my multiplayer fireball, the host can shot the spell in right camera facing direction but the player who join game are shotting in wrong way: they shot in host camera direction and not on their own.
    I really need help please, here are my script:
    Fireball script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class ShotSpellWar : NetworkBehaviour {
    7.  
    8.     [SerializeField] float power = 800f;
    9.     [SerializeField] GameObject Gaia;
    10.     [SerializeField] Transform magicspawn1;
    11.     public Camera cameratest;
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update() {
    19.         if (Input.GetKeyDown(KeyCode.E))
    20.         {
    21.             if (!isLocalPlayer)
    22.                 return;
    23.  
    24.             CmdGaia();
    25.         } }
    26.     [Command]
    27.     void CmdGaia()
    28.     {
    29.         GameObject instance = Instantiate(Gaia, magicspawn1.position, cameratest.GetComponent<Transform>().rotation) as GameObject;
    30.         //instance.GetComponent<Rigidbody>().AddForce(magicspawn1.forward * power);
    31.         //spellObject.AddComponent<Rigidbody>();
    32.         instance.GetComponent<Rigidbody>().useGravity = false;
    33.         instance.GetComponent<Rigidbody>().velocity = instance.transform.forward * power;
    34.         NetworkServer.Spawn(instance);
    35.     }
    36.  
    37. }
    38.  
    39.  
    Camera script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonCamera : MonoBehaviour {
    6.  
    7.     public Vector3 offset;
    8.  
    9.     public bool useOffsetValues;
    10.  
    11.     public float rotateSpeed;
    12.  
    13.     public Transform pivot;
    14.  
    15.     public float maxViewAngle;
    16.     public float minViewAngle;
    17.  
    18.     private const float Y_ANGLE_MIN = -80.0f;
    19.     private const float Y_ANGLE_MAX = 50.0f;
    20.     public Transform lookat;
    21.     public Transform camTransform;
    22.  
    23.     public Camera cam;
    24.  
    25.     public float distance = 5.0f;
    26.     public float currentX;
    27.     public float currentY;
    28.     public float sensivityX = 4.0f;
    29.     public float sensivityY = 1.0f;
    30.    
    31.     private void Start()
    32.     {
    33.         if (!useOffsetValues)
    34.         {
    35.             offset = lookat.position - transform.position;
    36.         }
    37.         pivot.transform.position = lookat.transform.position;
    38.         //pivot.transform.parent = target.transform;
    39.         //pivot.transform.parent = null;
    40.  
    41.         camTransform = transform;
    42.         //cam = Camera.main;
    43.  
    44.         //enlever pour debloquer souris
    45.         Cursor.lockState = CursorLockMode.Locked;
    46.  
    47.     }
    48.  
    49.     private void Update()
    50.     {
    51.    
    52.         currentX += (Input.GetAxis("Mouse X") * sensivityX);
    53.         float horizontal = Input.GetAxis("Mouse X") * sensivityX;
    54.         lookat.Rotate(0, horizontal, 0);
    55.         currentY -= (Input.GetAxis("Mouse Y") * sensivityY);
    56.         float vertical = Input.GetAxis("Mouse Y") * sensivityY;
    57.         pivot.Rotate(-vertical, 0, 0);
    58.  
    59.         currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
    60.  
    61.     }
    62.     private void LateUpdate()
    63.  
    64.     {
    65.  
    66.  
    67.         //pivot.Rotate(-vertical, 0, 0);
    68.      
    69.         float desiredYAngle = lookat.eulerAngles.y;
    70.         float desiredXAngle = lookat.eulerAngles.x;
    71.  
    72.         Vector3 dir = new Vector3(0, 0, -distance);
    73.         Quaternion localrotation = Quaternion.Euler(currentY, currentX, 0);
    74.         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    75.         transform.position = lookat.position - (rotation * offset);
    76.         camTransform.position = lookat.position + localrotation * dir;
    77.         camTransform.LookAt(lookat.position);
    78.        
    79.         transform.LookAt(lookat);
    80.  
    81.     }
    82.  
    83. }
    84.  
    Inspector:
    ShotProblem.png
    I need some network script on the camera ? Im really stuck ...
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well in CmdGaia you are using cameratest.GetComponent<Transform>().rotation to set the rotation. Since this code is only ever run on the server, it will only ever user the server's rotation of that camera. You should probably send the rotation you want as part of the Command.
     
    tijanikun likes this.
  3. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Thank you for your answer, but how can i send rotation as part of command please ? I tought it was already since that line is on the command
     
  4. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Please can you explain ?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So everything in the Command, including all variables used within it, is only run on the server. All values used are values from the server unless you send them from the client. You can send a rotation from the client either as a quaternion or a vector3 as you would when calling any other method.

    Code (csharp):
    1. void sendClientRotation()
    2. {
    3.     CmdSendClientRotation(transform.rotation);
    4. }
    5.  
    6. [Command]
    7. void CmdSendClientRotation(Quaternion clientRotation)
    8. {
    9.     //Do something on server here
    10. }
     
    Last edited: Dec 3, 2018
  6. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Im sorry i don't really understand, i need to send the rotation from the camera script and then use that rotation in the spell script ? How exactly ? What should i put on "//Do Something on...."

    Thank you for your time spent
     
  7. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Anyone please ?
     
  8. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    Still no solution, i bump it again sorry...
     
  9. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    Code (CSharp):
    1.  void Update() {
    2.         if (Input.GetKeyDown(KeyCode.E))
    3.         {
    4.             if (!isLocalPlayer)
    5.                 return;
    6.             CmdGaia(cameratest.transform.rotation);
    7.         } }
    8.     [Command]
    9.     void CmdGaia(Quaternion rot)
    10.     {
    11.         GameObject instance = Instantiate(Gaia, magicspawn1.position, rot) as GameObject;
    12.         //instance.GetComponent<Rigidbody>().AddForce(magicspawn1.forward * power);
    13.         //spellObject.AddComponent<Rigidbody>();
    14.         instance.GetComponent<Rigidbody>().useGravity = false;
    15.         instance.GetComponent<Rigidbody>().velocity = instance.transform.forward * power;
    16.         NetworkServer.Spawn(instance);
    17.     }
     
  10. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    129
    It works now, thank you!