• 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 Yohan_Games · Oct 18, 2012 at 05:36 PM · tagfollowrpgnearwhen

Target a GameObject and follow it when it's near

Hi Guys! I want to make a Strategy Zombie Survival RPG but, I have no idea how i shall make it work :/

It's Scripted in C#. I want the AI, Target a Tag when that tag Comes near. How shall i make it?

I've already got a Follow Script Here:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour
 {
     public int minRange;
     public bool follow;
     public float moveSpeed;
     public float rotationSpeed;
     private Transform myTransform;
     public Transform player;
 
     void Start()
     {
         myTransform = transform;
         player = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     void Update ()
     {
         if(player == null) {
             player = GameObject.FindGameObjectWithTag("Player").transform;
             if(player.name == "Cube (Transform)") {
                 player = GameObject.FindGameObjectWithTag("Player").transform;
             }
         }
         
         if(player != null && Vector3.Distance(player.position, myTransform.position) < minRange)
         {
             follow=true;
         }else{
             follow=false;
         }
 
         if(follow && player != null)
         {
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(player.position - myTransform.position), rotationSpeed * Time.deltaTime);
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         }
     }
 }
Comment
Add comment · Show 2
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 Yohan_Games · Oct 19, 2012 at 01:48 PM 0
Share

Really no idea :/?

avatar image Yohan_Games · Oct 19, 2012 at 02:55 PM 0
Share

Anyone out there?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lil_billy · Oct 19, 2012 at 01:57 PM

great thing about zombies is that they can have simple AI

you could simply have the zombie LookAt() your target in question and then go to him

to make this more dynamic you can use trigger colliders or masterful uses of raycast to set up sensing ranges to determine if a player is of a certain distance go to him or if hes gotten far enough away give up OnTriggerEnter() & Exit

or were you asking about what the AI should do when he gets to him

ex: create a spherical trigger collider that is like 10ft around your zombie character OnTriggerEnter(trigger:collider) { if(trigger.gameobject.tag.equals("Human") { follow } }

Comment
Add comment · Show 6 · 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 lil_billy · Oct 19, 2012 at 02:01 PM 0
Share

this would also not require you to look up all those players in the first place

avatar image Yohan_Games · Oct 19, 2012 at 02:05 PM 0
Share

it should be hard :/ but i had a script before that did this. but my brother broke the whole computer so i don't remember a

avatar image Yohan_Games · Oct 19, 2012 at 02:07 PM 0
Share

I Wonder if there is any Way to make like that The Zombie Target that tag which was in the area (sorry for my bad english)

avatar image lil_billy · Oct 19, 2012 at 02:10 PM 0
Share

yeah this does that because the if statement is checking the tags of the objects that enter and the trigger zone is the area that you are mentioning

if you are talking about physical areas you can just flip it and make the area itself a trigger zone and send$$anonymous$$essage to all zombies to go after a target player that entered

avatar image Yohan_Games · Oct 19, 2012 at 02:18 PM 0
Share

But is there any way to do it with just editing this script?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Particle follow object 1 Answer

Enemy Targetting Help 1 Answer

How to get a 3D TextMesh to face character? 1 Answer

Camera rotation around player while following. 6 Answers

Follow Player when Close C# Help 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges