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 RjToni · Sep 07, 2012 at 05:18 PM · ainavmesh

Setting An AI Routine

Hey, Guys!

So, I'm Learning Unity, so I'm having a hard time.

You know, I'm trying to program a simple rotine for my enemy bots, it's like this.

If the enemy doesn't spot the enemy, he'll just walk in a point a, then to the point b, just patroling, thinking about his robot life.

He'll walk to the point A, wait for 3 seconds, go to the point B, wait for more 3 seconds, back to the point A, and so on.

And if he's seeing the player, he'll pursuit him.

Anyway, i'm using NavMesh, to try to make that big boy walk around.

I just don't have a clue how to script this, could you help me?

Sorry for my english, I'm from Brazil

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Griffo · Sep 07, 2012 at 07:56 PM

Put this on your Bot, create some empty gameobjects for you waypoints, however many you want, fill in the fields in the inspector, make sure you Bot has a character controller on it, then move your player towards the Bot and he, or she should attack .. Not tested ..

Hope this helps.

            #pragma strict
             
             var waypoint : Transform[];            // The amount of Waypoint you want
             var patrolSpeed : float = 3;        // The walking speed between Waypoints
             var loop : boolean = true;            // Do you want to keep repeating the Waypoints
             var player : Transform;                // Referance to the Player
             var dampingLook = 6.0;                // How slowly to turn
             var pauseDuration : float = 0;        // How long to pause at a Waypoint
             var attackRange = 10;                // Range to start the attack
             var attackSpeed = 5.0;                // Speed to attack
             var attackLook = 10.0;                // How fast to turn when attacking
             
             private var distanceToPlayer : int;    // Distance from the enemy to the Player
             private var curTime : float;
             private var currentWaypoint : int = 0;
             private var character : CharacterController;
             private var gravity : float = 2.0;
             private var attacking : boolean = false;
             
             function Awake(){
             
             }
             
             function Start(){
             
                 character = GetComponent(CharacterController);
             }
             
             function Update(){
     
         distanceToPlayer = Vector3.Distance(player.position, transform.position);    // Distance between the enemy and the Player
     
         if(distanceToPlayer < attackRange){
             attacking = true;
             attack();
             }else{
             attacking = false;
             }
     
         if(currentWaypoint < waypoint.length && !attacking){
             patrol();
             }else{    
         if(loop && !attacking){
             currentWaypoint=0;
             } 
         }
     }
             
             function patrol(){
             
                     var target : Vector3 = waypoint[currentWaypoint].position;
                     target.y = transform.position.y; // Keep waypoint at character's height
                     var moveDirection : Vector3 = target - transform.position;
             
                 if(moveDirection.magnitude < 0.5){
     
                     if (curTime == 0)
                         curTime = Time.time; // Pause over the Waypoint
                     if ((Time.time - curTime) >= pauseDuration){
                         currentWaypoint++;
                         curTime = 0;
                     }
                 }else{        
             // Look at and dampen the rotation
                     var rotation = Quaternion.LookRotation(target - transform.position);
                     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
                     character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
                 }    
             }
             
             function attack(){
             // Attack the Player
             
             // Rotate to face the Player
             var lookPos = player.position - transform.position;
                 lookPos.y = 0;
                 var rotation = Quaternion.LookRotation(lookPos);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
                 
             // Move towards the Player
                 var target : Vector3 = player.position;
                 var moveDirection : Vector3 = target - transform.position;
             
                 moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis
                 moveDirection.y -= gravity; // Add gravity so the he always stays on the ground
                 character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
             }
         
Comment
Add comment · Show 2 · 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 RjToni · Sep 07, 2012 at 09:55 PM 0
Share

I've tested that, is working well, but, it's not facing the target, in that case, the waypoints

avatar image Griffo · Sep 08, 2012 at 06:27 AM 0
Share

Edited script, working now ..

avatar image
0

Answer by RjToni · Sep 17, 2012 at 02:13 AM

Thanks for the help, It's working ok, but, I'm having problems about collisions. I've attached a Sphere collider in the bot's sword, then, if that sphere collides with the player, then the player loses a point of HP, but the collision is not working, and I Hell don't know why!!

that's the AI script so far:

var waypoint : Transform[]; // The amount of Waypoint you want

         var patrolSpeed : float = 3;       // The walking speed between Waypoints

         var loop : boolean = true;       // Do you want to keep repeating the Waypoints

         var player : Transform;          // Referance to the Player

         var dampingLook = 6.0;          // How slowly to turn

         var pauseDuration : float = 0;   // How long to pause at a Waypoint

         var attackRange = 10;          // Range to start the attack

         var attackSpeed = 5.0;          // Speed to attack

         var attackLook = 10.0;          // How fast to turn when attacking

         var AtkSpeed = 1;

         



         private var distanceToPlayer : int; // Distance from the enemy to the Player

         private var curTime : float;

         private var currentWaypoint : int = 0;

         private var character : CharacterController;

         private var gravity : float = 2.0;

         private var attacking : boolean = false;

         private var isattacking : boolean = false;



         function Awake(){



         }



         function Start(){

         

             

             character = GetComponent(CharacterController);

             animation["attack"].speed = AtkSpeed;

             animation["attack"].layer = 1;

                   

         }



         function Update(){

         

     distanceToPlayer = Vector3.Distance(player.position, transform.position);  // Distance between the enemy and the Player



     if(distanceToPlayer < attackRange){

        attacking = true;

        attack();

        }else{

        attacking = false;

        }

        

     



     if(currentWaypoint < waypoint.length && !attacking){

        patrol();

        }else{    

     if(loop && !attacking){

        currentWaypoint=0;

         } 

     }

 }

         function patrol(){


                 var target : Vector3 = waypoint[currentWaypoint].position;

                 target.y = transform.position.y; // Keep waypoint at character's height

                 var moveDirection : Vector3 = target - transform.position;



             if(moveDirection.magnitude < 0.5){



                if (curTime == 0) {

                  curTime = Time.time; // Pause over the Waypoint

                  animation.Play("idle");

                  }

                if ((Time.time - curTime) >= pauseDuration){

                  currentWaypoint++;

                  curTime = 0;

                }

             }else{        

         // Look at and dampen the rotation

                var rotation = Quaternion.LookRotation(target - transform.position);

                transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);

                character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);

                animation.Play("walk");

             }  

         }


         function attack(){

         // Attack the Player

         if (distanceToPlayer <= 1) {

         animation.Play("attack");

         }

         else {

         animation.Stop("attack");

         }

            

         // Rotate to face the Player

         var lookPos = player.position - transform.position;

             lookPos.y = 0;

             var rotation = Quaternion.LookRotation(lookPos);

             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);



         // Move towards the Player

             var target : Vector3 = player.position;

             var moveDirection : Vector3 = target - transform.position;



             moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis

             moveDirection.y -= gravity; // Add gravity so the he always stays on the ground

             character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);

             animation.Play("run");

         }

         

And that's is the script that I've made for the sphere collider:

var Life = 3;

function OnCollisionEnter(theCollision : Collision){

if(theCollision.gameObject.tag=="Player") {

  Life -= 1;

}

}

function Update () {

print ("Health :" +Life); }

I'm really having problems to learn about collisions and triggers =/

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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make AICharacterController able to walk on walls and ceilings? 0 Answers

What’s the simplest way to find nearest navmesh targets? 0 Answers

Can you bake nav mesh in prefab mode? 0 Answers

Move NavMeshAgent at a constant speed 1 Answer

Navmesh path without agent 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