Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question How can i fix local multiplayer controller controlling both players?

Discussion in 'Multiplayer' started by Louis12345612, Sep 16, 2023.

?

How can i fix local multiplayer controller controlling both players?

  1. ..

    0 vote(s)
    0.0%
  2. .

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Louis12345612

    Louis12345612

    Joined:
    Nov 30, 2022
    Posts:
    1
    i am making a unity3d game about ragdolls where you can localy connect up to four players using controllers using the new input system, i have been trying to use the Player Input manager to do this but my controller is controlling both players, the script is bellow does anyone know how to fix this?
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;

    public class CharacterController : MonoBehaviour
    {
    [SerializeField] private float speed = 5f;
    [SerializeField] private ConfigurableJoint hipJoint;
    [SerializeField] private Rigidbody hip;

    [SerializeField] private Animator targetAnimator;

    private bool walk = false;

    // Store a reference to the Player Input component
    private PlayerInput playerInput;

    private void Awake()
    {
    // Get a reference to the Player Input component attached to this GameObject
    playerInput = GetComponent<PlayerInput>();
    }

    private void Update()
    {
    // Check if the Player Input component is not null
    if (playerInput != null)
    {
    // Get the movement input from the current player
    float horizontal = playerInput.actions["Move"].ReadValue<Vector2>().x;
    float vertical = playerInput.actions["Move"].ReadValue<Vector2>().y;

    Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

    if (direction.magnitude >= 0.1f)
    {
    float targetAngle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;

    this.hipJoint.targetRotation = Quaternion.Euler(0f, targetAngle + 90, 0f);

    this.hip.AddForce(direction * this.speed);

    this.walk = true;
    }
    else
    {
    this.walk = false;
    }

    this.targetAnimator.SetBool("Walk", this.walk);
    }
    }
    }