• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by digiumbri · Jun 28, 2020 at 06:14 AM · multiplayerunity 2dphotonvisual studio

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[PUN2] Problem at pushing the other players 1 Answer

Multiplayer board game using photon in unity (Assign 4 pawns to 1 photon player),Digital board game with Photon, assign 4 different coloured pawns to 1 player 0 Answers

Problem with PhotonView.RPC 4 Answers

Photon / Multiplayer 2 Answers

Photon - How to name a player? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges