• 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 /
  • Help Room /
avatar image
0
Question by Alipacman · Mar 30, 2017 at 08:17 PM · rotationnetworkingphoton

How to change Player rotation towards nearest other Player?

HelloI, I want the player to turn to the nearest other Player in the Photon Network, also with a turning Time, when pressing a Key.

I already have the script for turning towards an Object:

 _direction = (target.position - player.transform.position).normalized;
 _direction2 = Quaternion.LookRotation (_direction);
 player.transform.rotation = Quaternion.Slerp (transform.rotation, _direction2, Time.deltaTime * RotationSpeed);

But how can I set the target to be the nearest Player in the Photon Network?

Thank you for you time and help!

Comment
Add comment · Show 3
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 ChristianSimon · Apr 03, 2017 at 08:12 AM 0
Share

Hi,

well I guess this is independent from networking logic and more related to your game logic. For your local player you need to compare his distance to each other character and make the character with the shortest distance his 'look at' target.

avatar image Alipacman ChristianSimon · Apr 03, 2017 at 02:52 PM 0
Share

Can you tell me how to look for distances? And how can I get the GameObjects if they spawn in GameTime?

avatar image ChristianSimon Alipacman · Apr 03, 2017 at 03:33 PM 0
Share

You can use FindObjectOfType<PhotonView>() to get all game object which have a PhotonView component attached to them. Since those game objects also might be non-player objects you want to filter them additionally for example by using their tag attribute or something other that is unique to the character objects. When having all player objects you can use Vector3.Distance(Vector3 a, Vector3 b) to get the distance between your character and each other. While comparing distances, store the game object with the shortest distance locally in order to set the look-at target on that game object.

$$anonymous$$aybe some code will explain this better:

 List<GameObject> players = new List<GameObject>();
 
 foreach (PhotonView pView in FindObjectsOfType<PhotonView>())
 {
     if (pView.is$$anonymous$$ine)
     {
         continue;
     }
 
     if (pView.tag == "Player")
     {
         players.Add(pView.gameObject);
     }
 }
 
 GameObject nearest = null;
 float distance = float.$$anonymous$$axValue;
 
 foreach (GameObject go in players)
 {
     float d = Vector3.Distance(transform.position, go.transform.position);
 
     if (d < distance)
     {
         nearest = go;
     }
 }
 
 transform.LookAt(nearest.transform);

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

137 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

Related Questions

Rotation sync over network best practices (especially; direction) 0 Answers

How both players can control the soccer ball in 2 player football game 1 Answer

Sending RPC to specific photon players. 3 Answers

Photon - OnRoomJoined called before scene loads 0 Answers

How to instantiate the Player Prefab as an Object with PUN2,Accessing the Player as an Object with PUN2 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