- Home /
Photon Multiplayer problem - both characters moving with one control
For some reason when I'm moving one of my characters the other character also moves together. Both seem to be controlled by either one of the opened games. I'm new to coding so I have no idea what is causing the problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class Player : MonoBehaviourPunCallbacks
{
private Rigidbody2D myRigidbody;
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool facingLeft;
private bool attack;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
private bool jump;
[SerializeField]
private float jumpForce;
[SerializeField]
private bool airControl;
void Start()
{
facingLeft = true;
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
void Update()
{
HandleInput();
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
isGrounded = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleAttacks();
HandleLayers();
ResetValues();
}
private void HandleAttacks()
{
if (attack && isGrounded &&!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
myAnimator.SetTrigger("attack");
myRigidbody.velocity = Vector2.zero;
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
if (Input.GetKeyDown(KeyCode.E))
{
attack = true;
}
}
private void HandleMovement(float horizontal)
{
if (photonView.IsMine)
{
}
if (myRigidbody.velocity.y < 0)
{
myAnimator.SetBool("land", true);
}
if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack")&& (isGrounded || airControl))
{
myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);
}
if (isGrounded && jump)
{
isGrounded = false;
myRigidbody.AddForce(new Vector2(0, jumpForce));
myAnimator.SetTrigger("jump");
}
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void Flip(float horizontal)
{
if (horizontal < 0 && !facingLeft || horizontal > 0 && facingLeft)
{
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void ResetValues()
{
attack = false;
jump = false;
}
private bool IsGrounded()
{
if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
myAnimator.ResetTrigger("jump");
myAnimator.SetBool("land", false);
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!isGrounded)
{
myAnimator.SetLayerWeight(1, 1);
}
else
{
myAnimator.SetLayerWeight(1, 0);
}
}
}
Any help is appreciated. There are no apparent error codes so I'm more clueless as to how to proceed.
Answer by Captain_Pineapple · Jun 28, 2020 at 06:50 AM
Hey there,
this is basically one of the most common problems and there should be plenty of forum questions asking exactly this. Anyway: Your issue is that this script of yours is (probably) on both player objects. SO each player locally asks for input and then executes it.
In line 109 you already have your solution but in a somewhat useless place and empty(?). The "isMine" flag indicates if an object belongs to you as the local player. so for example in your Update you should check if(isMine) and only if that is true handle the input.
Answer by Pie556 · Oct 19, 2021 at 05:09 AM
captain Pineapple well can you give me an example bc i am stuck on this issue as well :) And Ik it has been a yr but if you would give an answer that would be nice :)
in update ,Check whether the operation is local. void Update() { if (!photonView.IsMine) { return; } HandleInput(); }
what @shejianshangdezhongguo says.
Since this can have other reasons in your case it is most the best to just open a new question with details for your specific issue.
You can then reference the questions by links to enable people to find possible answers.
Also if you read my post: the solution is already present in line 109.
Answer by Pavan-Soneji · May 22 at 05:41 AM
private void Awake() {
if(!photonview.isMine){
GetComponent<YourMovementScript>().enabled = false
}
}