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?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour {
public float maxSpeed = 5f;
public float rotSpeed = 180f;
float shipBoundaryRadius = 0.5f;
void Start () {
}
void Update () {
// ROTATE the ship.
// Grab our rotation quaternion
Quaternion rot = transform.rotation;
// Grab the Z euler angle
float z = rot.eulerAngles.z;
// Change the Z angle based on input
z -= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
// Recreate the quaternion
rot = Quaternion.Euler( 0, 0, z );
// Feed the quaternion into our rotation
transform.rotation = rot;
// MOVE the ship.
Vector3 pos = transform.position;
Vector3 velocity = new Vector3(0, Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
pos += rot * velocity;
// RESTRICT the player to the camera's boundaries!
// First to vertical, because it's simpler
if(pos.y+shipBoundaryRadius > Camera.main.orthographicSize) {
pos.y = Camera.main.orthographicSize - shipBoundaryRadius;
}
if(pos.y-shipBoundaryRadius < -Camera.main.orthographicSize) {
pos.y = -Camera.main.orthographicSize + shipBoundaryRadius;
}
// Now calculate the orthographic width based on the screen ratio
float screenRatio = (float)Screen.width / (float)Screen.height;
float widthOrtho = Camera.main.orthographicSize * screenRatio;
// Now do horizontal bounds
if(pos.x+shipBoundaryRadius > widthOrtho) {
pos.x = widthOrtho - shipBoundaryRadius;
}
if(pos.x-shipBoundaryRadius < -widthOrtho) {
pos.x = -widthOrtho + shipBoundaryRadius;
}
// Finally, update our position!!
transform.position = pos;
}
}