So I am making a 4-player game, and I have all the controls in the input manager reading from 4 controllers. The question I have now is how does each player character know what controller is theirs? Here is my character control code right now…
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 50f; //The force that will be applied to the player’s direction
private float speed; //The current speed of the player
public float JumpForce = 150f; //The force that will be applied when the player jumps
private float jumpPower = 0; //The current jump force of the player
public bool grounded; //Tracking if the player is on the ground
public bool jumping = false; //Tracking if the player is jumping
public Rigidbody RB; //The player’s RigidBody
void Start ()
{
RB = GetComponent(); //Set the player’s rigidbody
}
void Update ()
{
speed = Input.GetAxis(“Horizontal”) * moveSpeed; //Calculate the speed of the player, if not moving it will be 0, positive is right, negative is left
if (!jumping && Input.GetButtonDown(“Jump”)) //If the player is not currently jumping, and the jump button is pressed
{
jumping = true; //The player is jumping
jumpPower = JumpForce; //Apply the inputed Jump force to the current jump force
}
}
void FixedUpdate ()
{
RB.AddForce(new Vector3(speed, 0, 0)); // Move the player horrizontally
RB.AddForce(new Vector3(0, jumpPower, 0)); // Move the player up
if (jumping) // If the player is jumping
jumpPower = 0; // Reset the jump power so they don’t fly away
}
void OnCollisionEnter (Collision other)
{
if (other.collider.tag == “Ground”) // If the player is on the ground
{
grounded = true; // Set the player to grounded
jumping = false; // Set the player to not jumping
}
}
void OnCollisionExit(Collision other)
{
if (other.collider.tag == “Ground”) // If the player is leaving the ground
{
grounded = false; // Set the player to in the air
}
}
}