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 Quags · Aug 27, 2013 at 07:00 PM · c#aifollow playernotworking

AI to follow player not working

hey i found a post from awhile back about an AI script to follow the player. i tried to get it so it would work for C# but i just cant seem to figure it out. i know i made this sloppy but if you can figure it out please let me know. thanks. here is the code:

using UnityEngine; using System.Collections;

public class AIfollow : MonoBehaviour {

 public Transform : myTransform; //current transform data of this enemy
 private int target : Transform; //the enemy's target
 private int moveSpeed = 3; //move speed
 private int rotationSpeed = 3; //speed of turning
 
     
     function Awake()
     {
         myTransform = transform; //cache transform data for easy access/preformance
     }
 
     function Start()
     {
             target = GameObject.FindWithTag("Player").transform; //target the player
      }
 
     function Update () {
     //rotate to look at the player
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
            Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
         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 Quags · Aug 27, 2013 at 06:51 PM 0
Share

i dont know why its getting messed up when posting this but heres the coding

avatar image getyour411 · Aug 27, 2013 at 08:30 PM 0
Share

Are you getting an error on startup from this or is it not executing as you thought or what? One pitfall I see here is the use of transform.position to move something which usually yields very inconsistent results in a 3D hilly/uneven terrain.

3 Replies

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

Answer by SudoSandwich · Aug 30, 2013 at 05:33 AM

This is a very stripped down version of player tracking. The AI will come at the player but move through objects and not obey physics. Hope this helps.

If your trying to convert js to c# use Convert unity javascript (unityscript) to C# to get a base to start from then you just need to change a few things to get it working.

 using UnityEngine;
 using System.Collections;
 
 public class AIFollow : MonoBehaviour {
     
 public float Distance;
 public Transform Target;
 public float lookAtDistance= 25.0f;
 public float chaseRange= 15.0f;
 public float moveSpeed= 5.0f;
     
 /*
   This ai will fly and move through objects inlcuding terrain!
 */
 void  Update (){
 
     // Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
     Distance = Vector3.Distance(Target.position, transform.position);
     
     // AI begins tracking player.
     if (Distance < lookAtDistance)
     {
         lookAt();
     }
     
     // Attack! Chase the player until/if player leaves attack range.
     if (Distance < chaseRange)
     {Debug.Log("enemy chase");
         chase ();
     }
 
 }
 
 // Turn to face the player.
 void  lookAt (){
     // Rotate to look at player.
     Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
 
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
     //transform.LookAt(Target); alternate way to track player replaces both lines above.
 }
 
 void  chase (){
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);    
 }
 }


Comment
Add comment · Show 21 · 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 Quags · Aug 30, 2013 at 05:45 AM 0
Share

hey thanks for this but i put it all in right and it still just sits there and doesnt move

avatar image SudoSandwich · Aug 30, 2013 at 06:24 AM 0
Share

Did you link the Target variable to the player? It works, I tested it before posting. Put the script on the enemy. Drag your player from the hierarchy pane onto the Target variable in the inspector and it should work when you get close and are in front of the AI. Also be sure your AI is pointing the right way. I believe the blue arrow is forward. I dont have my laptop with me atm. I can help more later if you still need help.

avatar image SudoSandwich · Aug 30, 2013 at 06:25 AM 0
Share

What does the debugger say?

avatar image Quags · Aug 30, 2013 at 06:27 AM 0
Share

NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) AIfollow.Update () (at Assets/AIfollow.cs:25)

UnassignedReferenceException: The variable Target of 'AIfollow2' has not been assigned. You probably need to assign the Target variable of the AIfollow2 script in the inspector. UnityEngine.Transform.get_position () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) AIfollow2.Update () (at Assets/AIfollow2.cs:18)

where do i assign the target in this script?

avatar image SudoSandwich · Aug 30, 2013 at 12:44 PM 0
Share

alt text Target = your player object

With the enemy selected with the AI script attached drag the player object onto the AIFollow script where it says

Target None(Transform)

Alternatively you can click the little circle next to it and select the target (your player or and other object to follow) in the pop up list.

placetarget.png (492.8 kB)
Show more comments
avatar image
0

Answer by WizzDE · Aug 27, 2013 at 08:28 PM

If this should be in c# you want to relplace 'function' with 'void'. Also for your variables you want to have [type] [name] so for example public int Number = 1; private int Target : Transform; needs to be private Transfrom Target; The script should work fine.

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 Quags · Aug 28, 2013 at 07:34 PM 0
Share

thanks. everything is fine except still the line private int Transform Target; is still giving me errors.i tried everything with this line and no matter what im getting an error for it. do you know why?

avatar image
0

Answer by fafase · Aug 28, 2013 at 07:47 PM

You are declaring variable in a UnityScript way for a C# script:

  public Transform : myTransform;
  private int target : Transform; 

should be

  public Transform myTransform;
  private Transform target;
Comment
Add comment · Show 10 · 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 Quags · Aug 28, 2013 at 07:55 PM 0
Share

ok thanks i got that fixed but now im getting a problem with this line:

Quaternion.LookRotation(target.position - myTransform.position), (rotationSpeed*Time.deltaTime);

sorry for all these questions still new at this

avatar image perchik · Aug 28, 2013 at 07:56 PM 0
Share

What is the problem you're getting?

avatar image Quags · Aug 28, 2013 at 08:11 PM 0
Share

just unexpected symbols so its probably the littlest thing wrong but im not sure what if i have to get rid of the () or something. its on this line Quaternion.LookRotation(target.position - myTransform.position), (rotationSpeed*Time.deltaTime);

avatar image fafase · Aug 29, 2013 at 04:51 AM 0
Share

You have change your code I guess since you show:

  Quaternion.LookRotation(target.position - myTransform.position), (rotationSpeed*Time.deltaTime);

But had it different in the first place.

You are either missing a parenthesis or you have one too many

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime)**)**;

or

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime;
avatar image Quags · Aug 29, 2013 at 06:13 AM 0
Share

im still getting an error on that last line and i copied exactly what you have

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Basic AI Follow Player 2D C# 1 Answer

Complex Enemy follow AI 1 Answer

Distribute terrain in zones 3 Answers

How can I get a character to patrol and follow terrain? 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