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 Brodie Winning · Jan 20, 2011 at 04:15 AM · aiwaypoints

Why does this script not work?

Hey there below is my AI script that i've been working on for the last few months. It all works except for the guard function. Unity doesn't detect any errors so why won't anything happen.

//Moving var moveSpeed = 3.0;

//Transform Variables var target : Transform; var bulletPrefab : Transform; var dead : Transform; var waypoint1 : Transform; var waypoint2 : Transform; var waypoint3 : Transform; var waypoint4 : Transform;

//Attacking var attackRange = 60.0;

function Start () { Guard (); }

function Guard () { transform.LookAt(waypoint1); transform.position += transform.forward moveSpeed Time.deltaTime;

 distance = Vector3.Distance(transform.position, waypoint1.position); 

 if(distance <= 2.0)
 {
     transform.LookAt(waypoint2);
     transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }

 distance = Vector3.Distance(transform.position, waypoint2.position);

 if(distance <= 2.0)
 {
     transform.LookAt(waypoint3);
     transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }

 distance = Vector3.Distance(transform.position, waypoint3.position);

 if(distance <= 2.0)
 {
     transform.LookAt(waypoint4);
     transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }

 distance = Vector3.Distance(transform.position, waypoint4.position);

 if(distance <= 2.0)
 {
     transform.LookAt(waypoint1);
     transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }

}

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

 if(distance <= attackRange)
 {
     transform.LookAt(target);
     transform.position += transform.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 Brodie Winning · Jan 20, 2011 at 05:52 AM 0
Share

Yeah now it moves to the first waypoint but when it reaches the waypoint it just stops. Do you know why it won't move to the next. I've got a feeling it's to do with my if statements but I don't know how to fix the problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by 3DMagicVR · Jan 20, 2011 at 05:17 AM

Hi, don't use the guard function on the start function because they only works at the start game (only one time), on the other side use the update function this way the guard all ways be checked for the distance of the objects and then if it's in any of the settled ranges this should be work.

function Start() // work at the beginning.
function Update() // work all ways.

I don't know how you activate the Attack function but I suggest to don't use in a Awake or Start function to check a function to have to be checked all the time.

Comment
Add comment · 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
0

Answer by · Jan 20, 2011 at 05:37 AM

By calling Guard() in the Start() function, it's only happening once. So, if any of those if conditions are true on the first execution, they'll move ~0.48 units towards the waypoint. Otherwise, nothing will happen.

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 · Jan 20, 2011 at 05:38 AM 0
Share

Oops, it said 0 answers when I loaded this tab, and then wandered away before posting...

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

No one has followed this question yet.

Related Questions

Enemy AI monster problem 3 Answers

Follow up to AI Pathfinding Question 0 Answers

Bug with my enemyAI 2 Answers

Switching Enemy Behaviour through a set condition 0 Answers

How how can I make AI only move towards objects that are active in the scene 0 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