Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Phoskryfes · Sep 03, 2012 at 10:59 AM · playeraichangefollow

Change AI Follow Players

I am making a script that will make the AI follow the player. If the player is caught, it will follow the second player. If the second player is caught, it will chase the third player. And so on.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public int maxDistance;
     
     private Transform myTransform;
     
     void Awake() {
         myTransform = transform;
     }
     
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         
         target = go.transform;
         
         maxDistance = 1;
     }
     
     // Update is called once per frame
     void Update () {
         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
         
         //Look at target
         myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         
         if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
         //Move towards target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         }
     }
 }

That's what I got so far.

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
1
Best Answer

Answer by SomeGuy22 · Sep 03, 2012 at 02:06 PM

 var players = gameObject.FindGameObjectsWithTag("PlayerNotCaught");
     
     var distance = Mathf.Infinity; 
     var position = transform.position;
     // Iterate through them and find the closest one
     for (var go : GameObject in players)  {
         
         if (go != null)
         {
         var diff = (go.transform.position - position);
         var curDistance = diff.sqrMagnitude; 
         if (curDistance < distance) {
             
             target = go.transform;
             distance = curDistance;
         }
         }
     }

Put that code in the Update() Function, and get rid target = go in Start(). Change the tags of your players according to if they're caught or not. Not caught will have the tag "PlayerNotCaught" so the AI can determine them from the rest of the players.

This script will choose the closest not caught player, but if you want to go in order of player 1, 2, 3, 4, etc., you will have to use an if statement according to the current player in the array. Something like, if (player[i] == 1) else if (player[i] == 2) then target = player.

Comment
Add comment · Show 5 · 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 Phoskryfes · Sep 10, 2012 at 12:46 PM 0
Share

I like the second part of your paragraph, that is the exact thing that I need. But how do you write that on a script. The (player[i] ==1) etc. Should I still include the var players or should I replace them with The (player[i] ==1) or something.

avatar image SomeGuy22 · Sep 10, 2012 at 08:34 PM 0
Share

$$anonymous$$eep the var players (it's always good to hold a player list anyways) and have some sort of variable on the player script called something like "PlayerNumber" and then check if players[i].GetComponent(PlayerScript).playerNumber == 1

Specifically, you'd want to write a "for" line: http://answers.unity3d.com/questions/11547/how-do-i-use-for-to-create-loops-in-my-script-and.html

avatar image Phoskryfes · Sep 11, 2012 at 11:49 AM 0
Share

Should I make another script for the script that you just made? because That is in javascript.

avatar image SomeGuy22 · Sep 11, 2012 at 11:03 PM 0
Share

Oh my bad, you'll have to translate that into C sharp. I think it's pretty close to what I wrote.

Also you'll need another script if you don't already have a player script. You'll want to keep of the player numbers with function OnNetworkInstantiate ()

avatar image Montraydavis · Oct 30, 2012 at 08:44 AM 1
Share

Great, I am glad this helps ! Please feel free to thumbs up :)

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

9 People are following this question.

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

Related Questions

How do I make an object follow the player? 1 Answer

AI Help FPS 0 Answers

Problems with simple AI script 3 Answers

Player moves faster when fps is higher? 1 Answer

How to make an AI like slender ? 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