Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Camera.GetComponent(Transform).rotation on multiplayer problem

Discussion in 'Connected Games' started by tijanikun, Dec 6, 2018.

  1. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    28
    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. public class ShotSpellWar : NetworkBehaviour {
    6.     [SerializeField] float power = 800f;
    7.     [SerializeField] GameObject Gaia;
    8.     [SerializeField] Transform magicspawn1;
    9.     public Camera cameratest;
    10.     // Use this for initialization
    11.     void Start () {
    12.      
    13.     }
    14.     // Update is called once per frame
    15.     void Update() {
    16.         if (Input.GetKeyDown(KeyCode.E))
    17.         {
    18.             if (!isLocalPlayer)
    19.                 return;
    20.             CmdGaia();
    21.         } }
    22.     [Command]
    23.     void CmdGaia()
    24.     {
    25.         GameObject instance = Instantiate(Gaia, magicspawn1.position, cameratest.GetComponent<Transform>().rotation) as GameObject;
    26.         //instance.GetComponent<Rigidbody>().AddForce(magicspawn1.forward * power);
    27.         //spellObject.AddComponent<Rigidbody>();
    28.         instance.GetComponent<Rigidbody>().useGravity = false;
    29.         instance.GetComponent<Rigidbody>().velocity = instance.transform.forward * power;
    30.         NetworkServer.Spawn(instance);
    31.     }
    32. }
    33.  
    Camera script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ThirdPersonCamera : MonoBehaviour {
    5.     public Vector3 offset;
    6.     public bool useOffsetValues;
    7.     public float rotateSpeed;
    8.     public Transform pivot;
    9.     public float maxViewAngle;
    10.     public float minViewAngle;
    11.     private const float Y_ANGLE_MIN = -80.0f;
    12.     private const float Y_ANGLE_MAX = 50.0f;
    13.     public Transform lookat;
    14.     public Transform camTransform;
    15.     public Camera cam;
    16.     public float distance = 5.0f;
    17.     public float currentX;
    18.     public float currentY;
    19.     public float sensivityX = 4.0f;
    20.     public float sensivityY = 1.0f;
    21.  
    22.     private void Start()
    23.     {
    24.         if (!useOffsetValues)
    25.         {
    26.             offset = lookat.position - transform.position;
    27.         }
    28.         pivot.transform.position = lookat.transform.position;
    29.         //pivot.transform.parent = target.transform;
    30.         //pivot.transform.parent = null;
    31.         camTransform = transform;
    32.         //cam = Camera.main;
    33.         //enlever pour debloquer souris
    34.         Cursor.lockState = CursorLockMode.Locked;
    35.     }
    36.     private void Update()
    37.     {
    38.  
    39.         currentX += (Input.GetAxis("Mouse X") * sensivityX);
    40.         float horizontal = Input.GetAxis("Mouse X") * sensivityX;
    41.         lookat.Rotate(0, horizontal, 0);
    42.         currentY -= (Input.GetAxis("Mouse Y") * sensivityY);
    43.         float vertical = Input.GetAxis("Mouse Y") * sensivityY;
    44.         pivot.Rotate(-vertical, 0, 0);
    45.         currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
    46.     }
    47.     private void LateUpdate()
    48.     {
    49.         //pivot.Rotate(-vertical, 0, 0);
    50.    
    51.         float desiredYAngle = lookat.eulerAngles.y;
    52.         float desiredXAngle = lookat.eulerAngles.x;
    53.         Vector3 dir = new Vector3(0, 0, -distance);
    54.         Quaternion localrotation = Quaternion.Euler(currentY, currentX, 0);
    55.         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    56.         transform.position = lookat.position - (rotation * offset);
    57.         camTransform.position = lookat.position + localrotation * dir;
    58.         camTransform.LookAt(lookat.position);
    59.      
    60.         transform.LookAt(lookat);
    61.     }
    62. }
    63.  
    Inspector:

    I need some network script on the camera ? Im really stuck ...
     
  2. tijanikun

    tijanikun

    Joined:
    Aug 10, 2017
    Posts:
    28
    Please anyone ?