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
1
Question by adipatil3517 · May 10, 2017 at 02:24 PM · scripting problemnetworking

How to Make Enemy Follow Local Player only

I have created multiplayer Game. In that game I added Enemy AI. Now enemy follow the player but problem is that Whenever i create or start game and choose LAN host option the code work properly. Enemy follow the player. but when i add another client or another player everything get messed up.camera stop working enemy won't follow the player but as soon as i close all connected client the game again works fine. following is code . please suggest me something.

using UnityEngine; using System.Collections; using UnityEngine.Networking;

public class ArtificialIntelligence : NetworkBehaviour {

 public Transform target;         // Your Player
 public float Maxdistance = 5;          // Distance Enemy stops from player
 public int MoveSpeed = 5;        // How fast The Enemy will move
 public int RotationSpeed = 3;    // How fast The Enemy will rotate

 private Transform mytransform;

 void Awake ()
 {
     mytransform = transform;
 }

 void Update ()
 {
     //if(isLocalPlayer)
     {
         GameObject go = GameObject.FindGameObjectWithTag ("Player");
         target = go.transform;

         Debug.DrawLine (mytransform.position, target.position, Color.yellow);
         if (Vector3.Distance (target.position, mytransform.position) < 20) 
         {
             //Vector3 direction = target.position - mytransform.position;
             mytransform.rotation = Quaternion.Slerp (mytransform.rotation, Quaternion.LookRotation (target.position - mytransform.position), RotationSpeed * Time.deltaTime);

             Debug.Log ("Hii" + Vector3.Distance (target.position, mytransform.position));
             if (Vector3.Distance (target.position, mytransform.position) < Maxdistance) 
                 {
                     Debug.Log ("Hii" + Vector3.Distance (target.position, mytransform.position));
                     mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
                 }  
         }
     }
 }

}

thank you

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Varniukas · May 10, 2017 at 09:10 PM

void Update (){ if(isLocalPlayer){ return; } //your code goes here

Comment
Add comment · Show 3 · 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 adipatil3517 · May 11, 2017 at 06:09 AM 0
Share

thank you for providing the solution but the thing is if(isLocalPlayer) work for current object and i want to check "isLocalPlayer" for player.that is i want to check whether player is local or not. how to check that

avatar image Varniukas · May 11, 2017 at 01:00 PM 0
Share

https://unity3d.com/learn/tutorials/topics/multiplayer-networking

try here :)

avatar image adipatil3517 Varniukas · May 12, 2017 at 04:46 AM 0
Share

First of all i started to develop $$anonymous$$ultiplayer game from their only. Now i have completed all those basic things. now i am adding new things on my own and i am stuck here.because there is only one non networked zombi and i want it to follow only local player whoever goes near to him. or should i have to create enemy on client side so that everyone will have their own enemy and that will follow them locali. If it is so then tell me how to spawn enemy at client side. @Varniukas

avatar image
0

Answer by adipatil3517 · May 11, 2017 at 01:13 PM

First of all i started to develop Multiplayer game from their only. Now i have completed all those basic things. now i am adding new things on my own and i am stuck here.because there is only one non networked zombi and i want it to follow only local player whoever goes near to him. or should i have to create enemy on client side so that everyone will have their own enemy and that will follow them locali. If it is so then tell me how to spawn enemy at client side.@Varniukas

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UnityWebRequest post freezes in WebGL build 1 Answer

Unity Multiplayer game not working 0 Answers

How to check for a host in a certain area in a open world game? 0 Answers

how to sent all players to next level(lan server) 0 Answers

Unet Procedure Calls? 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