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. Dismiss Notice

How do I fix this problem?

Discussion in 'Scripting' started by Real_BTN4, Dec 8, 2020.

  1. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    I have a script for my game's characters' movement. Whenever 2 people add in they both control each other! Can someone help me make it so that the 2 players only control their own characters?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour {
    7.  
    8.     public float maxSpeed = 5f;
    9.     public float rotSpeed = 180f;
    10.  
    11.     float shipBoundaryRadius = 0.5f;
    12.  
    13.     void Start () {
    14.    
    15.     }
    16.    
    17.     void Update () {
    18.  
    19.         // ROTATE the ship.
    20.  
    21.         // Grab our rotation quaternion
    22.         Quaternion rot = transform.rotation;
    23.  
    24.         // Grab the Z euler angle
    25.         float z = rot.eulerAngles.z;
    26.  
    27.         // Change the Z angle based on input
    28.         z -= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
    29.  
    30.         // Recreate the quaternion
    31.         rot = Quaternion.Euler( 0, 0, z );
    32.  
    33.         // Feed the quaternion into our rotation
    34.         transform.rotation = rot;
    35.  
    36.         // MOVE the ship.
    37.         Vector3 pos = transform.position;
    38.        
    39.         Vector3 velocity = new Vector3(0, Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
    40.  
    41.         pos += rot * velocity;
    42.  
    43.         // RESTRICT the player to the camera's boundaries!
    44.  
    45.         // First to vertical, because it's simpler
    46.         if(pos.y+shipBoundaryRadius > Camera.main.orthographicSize) {
    47.             pos.y = Camera.main.orthographicSize - shipBoundaryRadius;
    48.         }
    49.         if(pos.y-shipBoundaryRadius < -Camera.main.orthographicSize) {
    50.             pos.y = -Camera.main.orthographicSize + shipBoundaryRadius;
    51.         }
    52.  
    53.         // Now calculate the orthographic width based on the screen ratio
    54.         float screenRatio = (float)Screen.width / (float)Screen.height;
    55.         float widthOrtho = Camera.main.orthographicSize * screenRatio;
    56.  
    57.         // Now do horizontal bounds
    58.         if(pos.x+shipBoundaryRadius > widthOrtho) {
    59.             pos.x = widthOrtho - shipBoundaryRadius;
    60.         }
    61.         if(pos.x-shipBoundaryRadius < -widthOrtho) {
    62.             pos.x = -widthOrtho + shipBoundaryRadius;
    63.         }
    64.  
    65.         // Finally, update our position!!
    66.         transform.position = pos;
    67.  
    68.     }
    69. }
    70.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
  3. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
  4. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23