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 Ian-McCleary · Apr 11, 2014 at 03:03 AM · javascripterror

Need Help Understanding Script! (Javascript)

Ok, So i am using an AI script in my game and i need a little help understanding a few of the components of the scrip. I am following a video tutorial series and i am trying to understand more about what the script means and how it works with the objects in game. (basically i did what the man in the tutorial did and i need some help understanding what is going on)

First off, what is the characterController variable doing? The AI object is supposed to move towards me and even when i am in range of it, it just does move at all! When in unity under the property inspector the script has an option to add some sort of controller to the character controller spot, but when i do that i can go within the lookatdistance. (see code below to answer)

If you know any other reason why it is not chasing me, please help it would be much appreciated.

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var attackRange = 15.0;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         renderer.material.color = Color.yellow;
         lookAt();
     }
     
     if (Distance > lookAtDistance)
     {
         renderer.material.color = Color.green;
     }
     
     if (Distance < attackRange)
     {
         renderer.material.color = Color.red;
         attack ();
     }
 }
 
 function lookAt ()
 {
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function attack ()
 {
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
 }

Comment
Add comment · Show 1
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 ToxxicSin · Apr 11, 2014 at 03:49 AM 0
Share

After you attach this script to the AI, make sure to pick the AI object and check to make sure all of the speed values and everything is set. And most definitely make sure the Player prefab/object is assigned to the Target variable

1 Reply

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

Answer by Foestar · Apr 11, 2014 at 03:49 AM

Read it in small snippets. First you have your variables.

Then in your update you have a variable being defined based off an objects transform. The distance is then determined between the desired object and the current object.

Then you have it check the distance which will then determine it's next action.

The first action is to look at the object. This is done with the Quaternion rotations. Essentially, it will just rotate the object to look at the controlled object.

Next action is the attack which then moves the object forwards toward the other one. But this only activates if the objects are in range.

Now as for the target, it is defined by adding a object in the inspector window that you were asking about.

Comment
Add comment · Show 3 · 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 Ian-McCleary · Apr 11, 2014 at 04:51 AM 0
Share

I have the target selected in unity as myself but it still doesnt move me...is there something with the character controller that needs to take place to follow me? or is the script just written wrong?

avatar image Ian-McCleary · Apr 11, 2014 at 05:06 AM 0
Share

Thanks for telling me what it meant, i did a little more exploring and found that i had the wrong controller on the AI!

avatar image Foestar · Apr 11, 2014 at 06:32 AM 0
Share

Glad you figured it out. The script is correct at first glance. It's just a matter of what it's attached to and what "Target" is defined as.

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

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

Related Questions

Multiple Cars not working 1 Answer

Error in distance coding 1 Answer

BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers

Errors with script using Javascript. 2 Answers

"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 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