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 PuzzleBox · Nov 01, 2011 at 05:36 PM · aivariablewaypoint

Clearing a destination...

How do I have my AI script stop moving towards its way-point while still moving forward? I'm trying to make my AI run in fear when clicked on. Right now, it runs, but he will make a large circle in order to keep going for the way-point.

I just tried replacing variable dir with "transform.forward speed Time.deltaTime;" when mouse clicks object but its not working.

The Full AI code:

 var speed = 6;
 var deer = 10;
 var tap = 100;
 var rotate;
 var target : Transform;
 private var waypoint : int;
 
 //Movement Script
 
 function Awake(){
 
 }
 
 function Update () {
     var dir = (target.position - transform.position).normalized;
     var hit : RaycastHit;
     if(Physics.Raycast(transform.position, transform.forward, hit, 15)){
         if(hit.transform != transform){
         dir += hit.normal * 50;
         }
     }
 
     var leftR = transform.position;
     var rightR = transform.position;
     
     leftR.x -= 2;
     rightR.x += 2;
     
     if(Physics.Raycast(leftR, transform.forward, hit, 8)){
         if(hit.transform != transform){
         dir += hit.normal * 50;
         }
     }
     
     if(Physics.Raycast(rightR, transform.forward, hit, 8)){
         if(hit.transform != transform){
         dir += hit.normal * 50;
         }
     }
     
     var rot = Quaternion.LookRotation(dir);
     
     transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
     transform.position += transform.forward * speed * Time.deltaTime;
     
 //Destroy Object at Destination
     if (target) {
         var dist = Vector3.Distance(target.position, transform.position);
             if(dist <= 5){
                 Destroy(this.gameObject);
             }
     }
     
 }   
 
 //Mouse Events
 function OnMouseEnter() {
     LevelController.Points += deer + 10;
 }
 
 function OnMouseDown () {
     LevelController.Points += tap + 100;
     speed = 25;
     ChangeDirection();
     yield WaitForSeconds (60);
     Destroy(this.gameObject);
 }
 
 //Random Direction Script
 function ChangeDirection() {
     rand = Random.Range(2,-2);
 
     switch(rand){
        case -2:
          rotate = -135;
        break;
 
        case -1:
          rotate = -45;
        break;
 
        case 1:
          rotate = 45;
        break;
 
        case 2:
          rotate = 135;
        break;
 
        default:
          rotate = 0;
        break;
        }
 
     if(rotate!=0){
        transform.Rotate(Vector3.up, rotate);
     }
 }
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
0

Answer by mirswith · Nov 01, 2011 at 09:48 PM

The script above looks like it is creating a direction to run towards 'target'. Using this script you would need to change the target of where you want the game object to move towards.

Comment
Add comment · Show 7 · 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 Rod-Green · Nov 01, 2011 at 09:55 PM 0
Share

That seems correct - so to force it to go forward all you'd need to do is set

 dir = transform.forward;

Then do your avoidance check..

 function Update () {
     var dir = (target.position - transform.position).normalized;
     var hit : RaycastHit;
     if(scared)
          dir = transform.forward;
     if(Physics.Raycast(transform.position, transform.forward, hit, 15)){
        if(hit.transform != transform){
        dir += hit.normal * 50;
     }
 }
avatar image mirswith · Nov 01, 2011 at 10:03 PM 0
Share

"dir" should already be the forward vector towards "target". If you wanted to run away from target then you could add a slightly different version of your addition:

if( scared ) dir = -dir;

avatar image Rod-Green · Nov 01, 2011 at 10:58 PM 0
Share

Oh what I understood was he just wanted it to keep running forward in it's current direction.. so the dir = transform.forward would remove any adjustment made to move towards the target and just maintain the current trajectory.

avatar image mirswith · Nov 01, 2011 at 11:11 PM 1
Share

Good catch, I think your right now that I re-read it. We are missing part of the picture here since it looks like this class does not update the transform itself. If he does not want to move toward the target anymore in the higher level code he could simply disconnect this script and install a new "run away" script.. :)

avatar image PuzzleBox · Nov 02, 2011 at 12:02 AM 0
Share

I did want to have the AI move forward in the direction the model is currently facing. I tried putting "transform.forward" in the function On$$anonymous$$ouseDown but it will not stop moving towards the way-point. Ideas? I was thinking of putting a way-point as a part of the prefab that the character is made of. Then change the way-point to that one to where it just acts like a carrot on a stick.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy Damage 2 Answers

Stay Back! 2 Answers

Shooting Damage Help 1 Answer

Store temp var / invert var 1 Answer

C# Declaring Variables Within If Statements 2 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