Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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

3 Replies

· 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
avatar image
0

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 :)

Comment
Add comment · Show 2 · 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
avatar image shejianshangdezhongguo · Oct 19, 2021 at 08:22 AM 1
Share

in update ,Check whether the operation is local. void Update() { if (!photonView.IsMine) { return; } HandleInput(); }

avatar image Captain_Pineapple · Oct 19, 2021 at 09:21 AM 0
Share

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.

avatar image
0

Answer by Pavan-Soneji · May 22 at 05:41 AM

 private void Awake() {
      if(!photonview.isMine){
            GetComponent<YourMovementScript>().enabled = false
      }
 }
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.

Follow this Question

Answers Answers and Comments

236 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 avatar image avatar image avatar image

Related Questions

Multiplayer game : bullets spawning and direction point is shows different with each 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

[PUN2] Problem at pushing the other players 1 Answer

Having only one of the same gameobjects in a scene. Photon Pun 1 Answer

photon player freak out! 1 Answer


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