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. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Multiplayer Look at mouse

Discussion in 'Connected Games' started by Nexxy, Dec 31, 2018.

  1. Nexxy

    Nexxy

    Joined:
    Mar 17, 2017
    Posts:
    2
    Hey!
    I made a 2D simple platformer, and the character has a gun. The gun always looking at the mouse. In multiplayer, the other clients' gun rotating to the same direction.
    How can I fix it?
    The code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerGun : MonoBehaviour
    6. {
    7.    
    8.     public float offset;
    9.    
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    18.         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    19.         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
    20.     }
    21. }
    22.  
     
  2. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    2,815
    Read the UNET manual. Use NetworkBehaviour + isLocalPlayer checks. Send direction to server to sync to other clients.
     
  3. rickikki

    rickikki

    Joined:
    Oct 9, 2018
    Posts:
    4
    Thank for you help have fixed party of probleme .

    I have split gun and player game object, i give network Id and network transform each.

    On void start of the player controller have Instantiate the gun .

    The gun have script for follow the mousse .

    And player ObjectToFollow.
    But dont worker for follow player .

    I need to find how make one game object follow the player prefab.

    If you have good tuto for learn unet .


    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7.  
    8.  
    9. public class GunScript : NetworkBehaviour {
    10.  
    11.  
    12.     public GameObject objectToFollow;
    13.  
    14.     public float speed = 10.0f;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.  
    20.     }
    21.  
    22.     void Update () {
    23.  
    24.  
    25.         Vector2 position = transform.position;
    26.         position.y = objectToFollow.transform.position.y;
    27.         position.x = objectToFollow.transform.position.x;
    28.  
    29.         transform.position = position;
    30.  
    31.  
    32.  
    33.         Vector2 direction = (Input.mousePosition) - transform.position;
    34.         float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
    35.         Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.forward);
    36.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, speed * Time.deltaTime);
    37.    
    38. }
    39. }
    40.  
    41. [CODE/]

     
    Last edited: Jan 6, 2019