Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 acclogin71 · Aug 01, 2018 at 02:20 PM · playermultiplayer-networkingcamera follow

how to find local player in multiplayer game?

so I have trouble finding local player to attach camera to it.
I used following script to make camera follow the player

  //attach this script to camera
 GameObject Player;
 public Vector3 offset;
 void Update ()
 {
     Player = GameObject.FindWithTag("Player");
     transform.position = Player.transform.position + offset;
 }


notice that find game object is in update method and not in start method because when game starts there is no player in scene and when player spawns in the scene, script couldnt find it because it only looks once. also notice I'm finding player with its tag. so with this script when I hit play and spawn player, camera does follow player.
.
but the problem is script doesnt know which one is local player so it starts following the first player that spawns in the scene. when client player joins the server, camera of client player hovers over host player only and doesnt follow that client player. so I used following script to make camera follow local player only.

 //attach this script to camera instead of one above.
 GameObject Player;
 public Vector3 offset;
 void Update ()
 {
     Player = GameObject.FindWithTag("Player");
     if (isLocalPlayer)
     {
         transform.position = Player.transform.position + offset;
     }
     else
     {
         return;
     }
 }

(FYI because this script is driven by network behavior camera has network identity component attached to it and I have tick marked local player authority). and suddenly camera doesnt follow either of the player, ie host player and client player. that means it couldnt find gameobject with "player" tag on it. or maybe problem is something else I dont know.
what I think is happening with 2nd script is with isLocalPlayer function it is finding camera itself as a local player (because local player authority is tick marked) and not the actual player of the game.
.
so may be this is not how you find local player. so I'm looking for a way to find local player and make my camera follow it. ie host camera follow host player and client camera follow client player. thank you in advance.

Comment
Add comment · Show 1
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 acclogin71 · Aug 01, 2018 at 02:25 PM 0
Share

sorry, I forgot to mention one thing. Camera is not child of player. and I dont want to make it that way. it is top down racing game so I dont want camera to rotate with player.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bubinga_Studios · Aug 01, 2018 at 03:11 PM

What I would do is include the camera and player in the same Player Prefab, if you are trying to make an individual view. Like the picture below.alt text The script I use to make smooth camera movement is a Vector3.Lerp. Here is the simplified code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FixCamToPlayer : MonoBehaviour {
 public Transform Player;
 public float Speed = 1;
 public Transform Pivot;
     // Use this for initialization
     
     // Update is called once per frame
     void FixedUpdate () {
         if(Player != null) {
         Pivot.position = Vector3.Lerp(Pivot.position, Player.position, Speed);
                             }
     }
 }
 

A Speed value of 1 will lock it onto the character rather than moving towards player. Using fixed update, it will update every frame, to keep it clean.

If I did not answer your question the way you wanted, let me know! Hope this helps!


multiplayercamsetup.png (3.4 kB)
Comment
Add comment · Show 1 · 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 acclogin71 · Aug 02, 2018 at 10:45 AM 0
Share

hi, thanks for the reply. I tried your method but unfortunately it didnt work. I ran into all sorts of new problems. so here's things I did.

  1. made empty game object, CP

  2. made camera and player child of CP

  3. added network identity component to CP (because network manager only spawns gameobjects that has network identity attached to it.)

  4. attached your script to player with $$anonymous$$or adjustments, as follows
    public Transform Player;
    public float Speed = 1;
    public Transform Pivot;
    public Vector3 offset;
    void FixedUpdate()
    {
    if (Player != null)
    {
    Pivot.position = Vector3.Lerp(Pivot.position, Player.position + offset, Speed);
    }
    }

    offset because camera spawns inside the player.

  5. assigned player to player and main camera to pivot

  6. made a if(!isLocalPlayer) script to disable player and camera movement and attached it to CP

  7. when I hit play and host player spawns, everything looks fine.

  8. but when client player joins the scene, client camera does follow client player, but host camera suddenly stops following host player. I couldnt figure out what problem is with this one.

  9. also network transform is attached to player (not to CP) but movement of individual player is not translated.

  10. so basically with your method, client camera does follow client player, but host camera stops following host player after client joins in.

  11. how did it worked for you did you face any problems with this method?

  12. thanks again for your response.

avatar image
0

Answer by dontdiedevelop · Aug 02, 2018 at 11:03 AM

 //attach to camera
 public class CameraSc : NetworkBehaviour
 {
      GameObject Player;
      public Vector3 offset;
 
      public void SetPlayer(Transform newPlayer)
      {
          Player = newPlayer;
      }

      void Update ()
      {
          if(!isLocalPlayer)
              return;
          if(!Player)
              return;

          transform.position = Player.transform.position + offset;       
       }
  }
 
 //attach to player
 public class PlayerSc : NetworkBehaviour
 {
     void Start()
    {
        if(!isLocalPlayer)
           return;

         if(GameObject.FindObjectOfType<CameraSc>())
             GameObject.FindObjectOfType<CameraSc>().SetPlayer(transform);
        else
             Debug.LogError("Camera Not Found");
    }
 }




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

107 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

Related Questions

Photon server how to control player traffic in a client network 2 Answers

Creating a client player and a host player in a single device and networking them correspondingly 0 Answers

my multiplayer game second player not active 0 Answers

How Do Camera Follow Mullti Playeres 2 Answers

How to Represent Player Objects in Unity Multiplayer Board Game 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