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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character Controls broken?

Discussion in 'Multiplayer' started by Necktron, May 23, 2016.

  1. Necktron

    Necktron

    Joined:
    Feb 10, 2016
    Posts:
    20
    I'm making a small RPG, this got an Offline and an Online mode. In Offline, the player walks around perfectly with an Orbit Camera around him. The same should work in Online, (Online Player got Network Transform and all that stuff added to it). The problem is that when I host a server and start to walk around, the character only walks in the directions of the world, North + East + West + South. The camera is orbiting, but the player will always go North on W, South and S, West on A, and East on D. The character Im using is currently the Unity 3D character Ethan.

    This is the code for the orbital camera:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [AddComponentMenu("Camera-Controller/Mouse Orbit with zoom")]
    5. public class CameraController : MonoBehaviour
    6. {
    7.     public Transform target;
    8.  
    9.     [SerializeField]
    10.     public float distance;
    11.     public float xSpeed;
    12.     public float ySpeed;
    13.     public float yMinLimit;
    14.     public float yMaxLimit;
    15.     public float distanceMin;
    16.     public float distanceMax;
    17.  
    18.     private Rigidbody rBody;
    19.  
    20.     float x = 0.0f;
    21.     float y = 0.0f;
    22.  
    23.     void Start()
    24.     {
    25.         Vector3 angles = transform.eulerAngles;
    26.         x = angles.y;
    27.         y = angles.x;
    28.  
    29.         rBody = GetComponent<Rigidbody>();
    30.    
    31.         if(rBody != null)
    32.         {
    33.             rBody.freezeRotation = true;
    34.         }
    35.     }
    36.  
    37.     void LateUpdate()
    38.     {
    39.         if(target)
    40.         {
    41.             x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
    42.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    43.  
    44.             y = ClampAngle(y, yMinLimit, yMaxLimit);
    45.  
    46.             Quaternion rotation = Quaternion.Euler(y, x, 0);
    47.  
    48.             distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
    49.  
    50.             RaycastHit hit;
    51.             if (Physics.Linecast (target.position, transform.position, out hit))
    52.             {
    53.                 distance -= hit.distance;
    54.             }
    55.  
    56.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    57.             Vector3 position = rotation * negDistance + target.position;
    58.  
    59.             transform.rotation = rotation;
    60.             transform.position = position;
    61.         }
    62.     }
    63.  
    64.     public static float ClampAngle(float angle, float min, float max)
    65.     {
    66.         if (angle < -360f)
    67.         {
    68.             angle += 360f;
    69.         }
    70.  
    71.         if (angle > 360f)
    72.         {
    73.             angle -= 360f;
    74.         }
    75.         return Mathf.Clamp(angle, min, max);
    76.     }
    77. }
    Is it an other way of doing this in Online? Have I missed something? What can I improve?

    I would love fast awnsers! <3
     
  2. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    Try starting with the camera in a different angle and seeing if that affects the locked axis.
    If it does it means the camera isn't being moved/detected as moving.
    Otherwise, if it doesn't change the locked axis, then it might just be detecting the camera as sitting at (0,0,0) or something.