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 Nahoyman78 · Apr 22, 2012 at 04:16 PM · playeraitagfollowclose

Follow Player when Close C# Help

Hi Guys! :) I have a script here and i need help how do i do so The AI Follows player just when the player is Close? I have a Script Temp Already here :D :

SCRIPT

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;
     }
 }

}

Any Ideas :)?

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 Owen-Reynolds · Apr 22, 2012 at 04:21 PM 0
Share

What is the specific problem: What does it do now and how do you want that to change?

This looks like it should move closer until it gets to 1 unit away, which seems fine (but it measures center to center, so if you used normal sized cubes, 1 unit away is touching.)

avatar image Nahoyman78 · Apr 22, 2012 at 04:25 PM 0
Share

I want to make so If a Player Touches/Walk near the Enemy. It starts to Follow it until the enemy / player dies

avatar image Nahoyman78 · Apr 22, 2012 at 04:34 PM 0
Share

Any Ideas? :)

1 Reply

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

Answer by fafase · Apr 22, 2012 at 05:14 PM

Ok, this would be a simple version. You have your target (player) that you drag in the inspector. Then it compares the position of each and if smaller than a range the NPC looks at the player and walk towards him. If the NPC gets too close, it stops walking. This way your guy is keeping its distance from the player and just follows him around.

 var target :Transform;
 var maxRange:int;
 var minRange:int;
 
 if((Vector3.Distance(transform.position,target.position)<maxRange)
    && (Vector3.Distance(transform.position,target.position)>minRange)){
       transform.LookAt(target);
       transform.Translate(Vector3.forward * Time.deltaTime);
 }

Now if you want the NPC to follow no matter what

 var target :Transform;
 var minRange:int;
 var follow:boolean;
 
 function Update(){
 if(Vector3.Distance(transform.position,target.position)<minRange)
    follow=true;
 if(follow){
    transform.LookAt(target);
    transform.Translate(Vector3.forward * Time.deltaTime);}
 }

Now your guy will start follow when you get close and since the boolean never changes it will follow you till death.

Comment
Add comment · Show 9 · 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 Nahoyman78 · Apr 22, 2012 at 05:14 PM 0
Share

thanks buddy :)

avatar image Nahoyman78 · Apr 22, 2012 at 05:17 PM 0
Share

but i get a Error:

Assets/Scripts/AI Follow.js(7,13): BCE0005: $$anonymous$$ identifier: 'Distance'.

avatar image fafase · Apr 22, 2012 at 05:19 PM 0
Share

Ooops Vector3.Distance()

avatar image fafase · Apr 22, 2012 at 05:20 PM 0
Share

And it is JS, you will have to convert...

avatar image Nahoyman78 · Apr 22, 2012 at 05:23 PM 0
Share

Yeah. Thanks :)

Show more comments

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

6 People are following this question.

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

Related Questions

AI Follow Player "Tag" Help 1 Answer

Follow Player Via Waypoints 1 Answer

AI Help FPS 0 Answers

Camera rotation around player while following. 6 Answers

Change AI Follow Players 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