Player is not rotating towards the mouse (Unity Photon Pun2)

Viewed 308

I am a quite new to programing and networking, so I'm currently stuck on a problem that regards Unity and Photon 2. The problem is the players in my multiplayer game are not rotating towards the mouse. When I spawn in the players, I put the camera as the child of player which I think is the problem. One of the players:

PlayerPrefab Image

Here is my player movement code:

public class MultiplayerMovement : MonoBehaviour
{
    
    private PhotonView PV;
    public float speed = 5f;
    public Rigidbody2D rb;
    public Camera cam;
    Vector2 movement;
    Vector2 mousePos;

    void Start()
    {   
       PV = GetComponent<PhotonView>();


        if(!PV.IsMine)
        {
          Destroy(GetComponentInChildren<Camera>().gameObject);
        }
    }

    void Update()
    {
        if(!PV.IsMine)
        {
            return;
        }

        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        print(mousePos);
    }

    void FixedUpdate()
    {
        if(!PV.IsMine)
        {
            return;
        }

        Move();
        
    }

    void Move()
    {
        rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
        Vector2 lookDir = mousePos - rb.position;
        float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
        rb.rotation = angle;
    }
}

When tried to find a solution, I didn't see people putting the camera as the child of the player, but instead they put it directly in the scene. I don't know how make the multiplayer work if I do it like that. I tried to set the camera position and rotation as player postion and rotation, but that didn't work. To simplify, I wish to add top down movement in this video: https://www.youtube.com/watch?v=LNLVOjbrQj4 (With moveable camera)

But in multiplayer with Photon 2. If you could help out, that would be great. Thanks.

Also, if print out the mouse position every update, it prints the the same mouse position even though I am moving my mouse. The mouse does update, however, when I move my player.

A weird quirk to the the player controls is that if you press the down arrow and the right arrow, for example, it goes southeast (down-right). I didn't intend to do it, but for some reason that happens.

0 Answers
Related