i am controlling all the players instead of one

Options

when i press my controller input all of the players move at once



this is the code

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using Photon.Pun;


public class PlayerMovement : MonoBehaviour

{

    [Header("Movement")]

    public float moveSpeed;


    public float groundDrag;


    public float jumpForce;

    public float jumpCooldown;

    public float airMultiplier;

    bool readyToJump;


    [HideInInspector] public float walkSpeed;

    [HideInInspector] public float sprintSpeed;


    [Header("Keybinds")]

    public KeyCode jumpKey = KeyCode.Space;


    [Header("Ground Check")]

    public float playerHeight;

    public LayerMask whatIsGround;

    bool grounded;


    public Transform orientation;


    float horizontalInput;

    float verticalInput;


    Vector3 moveDirection;


    Rigidbody rb;


    [HideInInspector] public TextMeshProUGUI text_speed;


    PhotonView view;


   

   private void Awake()

 {

     if (!view.IsMine && GetComponent<PlayerMovement>() != null)

     {

         

     }

 }

   

    private void Start()

    {

       

        view = GetComponent<PhotonView>();

        rb = GetComponent<Rigidbody>();

        rb.freezeRotation = true;


        readyToJump = true;

    }


    private void Update()

    {

       

              // ground check

        grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);


        MyInput();

        SpeedControl();


        // handle drag

        if (grounded)

            rb.drag = groundDrag;

        else

            rb.drag = 0;

       

     

    }


    private void FixedUpdate()

    {

        MovePlayer();

    }


    private void MyInput()

    {

        horizontalInput = Input.GetAxisRaw("Horizontal");

        verticalInput = Input.GetAxisRaw("Vertical");


        // when to jump

        if(Input.GetKey(jumpKey) && readyToJump && grounded)

        {

            readyToJump = false;


            Jump();


            Invoke(nameof(ResetJump), jumpCooldown);

        }

    }


    private void MovePlayer()

    {

        // calculate movement direction

        moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;


        // on ground

        if(grounded)

            rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);


        // in air

        else if(!grounded)

            rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);

    }


    private void SpeedControl()

    {

        Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);


        // limit velocity if needed

        if(flatVel.magnitude > moveSpeed)

        {

            Vector3 limitedVel = flatVel.normalized * moveSpeed;

            rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);

        }


        text_speed.SetText("Speed: " + flatVel.magnitude);

    }


    private void Jump()

    {

        // reset y velocity

        rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);


        rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);

    }

    private void ResetJump()

    {

        readyToJump = true;

    }

}

Answers

  • Tobias
    Options

    Have a look at the PUN Basics Tutorial. It explains from scratch, how to setup a prefab for users, how to control only one character and more.

  • byteez
    Options

    yes but it mixed me up more

  • Homertimes
    edited January 2023
    Options

    use if(getCompenent<PhotonView>().isMine) before the input function


    if(getCompenent<PhotonView>().isMine) => permit to limite de code to your own player