• 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 OrientalTaste · Jul 21, 2010 at 07:33 AM · aifollow

Henchmen Pet system

Total Unity noob, but I'd like to know how to start making a system where a pet or a henchman companion will follow the player around. I don't want someone to code the system for me just a few pointers would be great.

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

Answer by Julian-Glenn · Jul 21, 2010 at 07:49 AM

I did a down and dirty AIBO pet thing that followed my hero around. It is a hack job on the AI script from the FPS tutorial. IT also may have some vars lying around that aren't actually used but if attached to a mesh with a character controller component and Audio Source attached, and a hero tagged 'Player" it will work.

It was a project I dropped so I never went further with it but it will play animations and sound whilst following and would be pretty easy to expand upon to incorporate "Loyalty" "Happiness" "Learning Ticks" or those kind of things that pets and henchmen do :).

The script may look like Greek to you if you are really new to Unity or game scripting but if you work through a few of the Unity tutorials or even watch a few ToronadoTwins youtube vids it may help you out.

var speed = 3.0; var rotationSpeed = 5.0; var minFollowRange = 30.0; var dontComeCloserRange = 4.0; var waitRange = 6.0; var target : Transform; var idleSound : AudioClip; var chaseSound : AudioClip; var attackSound : AudioClip; var soundVolume : float = 1.0; var damage = 1; var hitEffect : GameObject; private var lastHitTime = 0.0;

// Make sure there is always a character controller @script RequireComponent (CharacterController)

function Start () {

 // Auto setup player as target through tags
 if (target == null && GameObject.FindWithTag("Player"))
     target = GameObject.FindWithTag("Player").transform;
 Patrol();

}

function ReConnect() { if (GameObject.FindWithTag("Player")) Debug.Log("Reconnecting..."); target = GameObject.FindWithTag("Player").transform; //Patrol(); }

function Patrol () { //Start idle sound audio.Stop();

  var curPlayerPos = target.position;
 while (true) {

     if (CanSeeTarget ())
         yield StartCoroutine("FollowPlayer");

 }

}

function CanSeeTarget () : boolean { if (target != null) return true; }

function AIBOPlay () { //Stop Audio audio.Stop();

 var direction = target.position - transform.position;
 direction.y = 0;
 // Start idle animation
 animation.CrossFade("idle",0.4);    
 // Rotate towards the target
 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

//Delay following player until dontComeCloserRange time 2 - solved a stutter problem if (Vector3.Distance(transform.position, target.position) > dontComeCloserRange * 2) yield StartCoroutine("FollowPlayer"); }

function FollowPlayer() { //Start The Chase

 var lastVisiblePlayerPosition = target.position;
 while (true) {
     if (target == null) 
         target = GameObject.FindWithTag("DeadPlayer").transform;

         var distance = Vector3.Distance(transform.position, target.position);

         lastVisiblePlayerPosition = target.position;

             if (distance > dontComeCloserRange ) {
             MoveTowards (lastVisiblePlayerPosition);
             }else{
             RotateTowards(lastVisiblePlayerPosition);
             }

         // Start playing with player if close and n sight
         if (distance < dontComeCloserRange )
             yield StartCoroutine("AIBOPlay");
     yield;
     }

}

function MoveTowards (position : Vector3) {

 var direction = position - transform.position;
 direction.y = 0;
 animation.CrossFade("run", 0.0);
 if (direction.magnitude < 0.5) {
     SendMessage("SetSpeed", 0.0);
     return;
 }
 // Rotate towards the target
 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
 // Modify speed so we slow down when we are not facing the target
 var forward = transform.TransformDirection(Vector3.forward);
 var speedModifier = Vector3.Dot(forward, direction.normalized);
 speedModifier = Mathf.Clamp01(speedModifier);

 // Move the character
 var distance = Vector3.Distance(transform.position, target.position);
 if (distance > waitRange) {
     if (!audio.isPlaying){ 
        audio.Stop();    
         audio.clip = chaseSound;
         audio.loop = false;
         audio.Play();
     }
 }
 if (distance > minFollowRange  ) {
 speed = 8.0;
 }else if (distance < waitRange){
 speed = 2.0;
 }else{
 speed = 5.0;
 }
 direction = forward * speed * speedModifier;
 GetComponent (CharacterController).SimpleMove(direction);
 //SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);

}

function RotateTowards (position : Vector3) {

 var direction = position - transform.position;
 direction.y = 0;
 if (direction.magnitude < 0.1)
     return;

 // Rotate towards the target
 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

}

function OnDrawGizmosSelected () { Gizmos.color = Color.green; Gizmos.DrawWireSphere (transform.position, waitRange); Gizmos.color = Color.red; Gizmos.DrawWireSphere (transform.position, minFollowRange); Gizmos.color = Color.blue; Gizmos.DrawWireSphere (transform.position, dontComeCloserRange); }

Comment
Add comment · Show 1 · 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 OrientalTaste · Jul 21, 2010 at 08:04 AM 0
Share

It works. And thanks for the link to the tutorials.

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

No one has followed this question yet.

Related Questions

Following AI - Similar to Snake Game 2 Answers

My AI code is only making a sprite look Left or Right? Help please. 1 Answer

How I can make a simple follow AI in Unity 5? 1 Answer

Raise AI aim 2 Answers

Advice on my Ship AI... 4 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