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 HollaKoala · Feb 27, 2013 at 08:15 PM · aigravitygrounded

Best way to keep an ai grounded?

Hi so right now I have a basic script that patrols waypoints and follows and attacks a player when it gets near. I have a script that tries to keep it grounded but it is buggy and doesn't work all the time. I was wondering what's the best way to keep the ai on the ground.

 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 ASpeed = 1;
 
 private var distanceToPlayer : int; // Distance from the enemy to the Player
 private var currentTime : 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;
 private var captured : boolean = false;
 
     function Awake(){
 
     }
     
     function Start(){
         character = GetComponent(CharacterController);
         animation["bite"].speed = ASpeed;
         animation["bite"].layer = 1;
     }
     
     function Update(){
         // Gets the distance between the enemy and the Player    
         distanceToPlayer = Vector3.Distance(player.position, transform.position);
         if(captured){
              animation["Death"].wrapMode = WrapMode.ClampForever;
              animation.Play("Death");
            }
         else if(distanceToPlayer < attackRange){
            attacking = true;
            attack();
            }else{
            attacking = false;
            }
             if(currentWaypoint < waypoint.length && !attacking && !captured){
                patrol();
                }else{    
                 if(loop && !attacking){
                    currentWaypoint=0;
                 } 
             }
     }
     function patrol(){
         var target : Vector3 = waypoint[currentWaypoint].position;
         target.y = transform.position.y; // This keeps the waypoint at character's height
         var moveDirection : Vector3 = target - transform.position;
         if(moveDirection.magnitude < 0.5){
            if (currentTime == 0) {
              currentTime = Time.time; // Pause over the Waypoint
              animation.Play("idle");
            }
            if ((Time.time - currentTime) >= pauseDuration){
              currentWaypoint++;
              currentTime = 0;
            }
         }else{        
             // This gets the location of the waypoint and rotates the AI to look at the waypoint and dampen the rotation
            var rotation = Quaternion.LookRotation(target - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
            moveDirection.y = 0;
            moveDirection.y -= gravity; //Gravity so the the AI  stays on the ground
            character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
            animation.Play("trot");
         }  
     }
  
     function attack(){
         // Attack the Player
         if (distanceToPlayer <= 2) {
             animation.Play("bite");
         } else {
         animation.Stop("bite");
         // 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; // This helps stop the AI running up off the floor to match the Players Y axis
         moveDirection.y -= gravity; //Gravity so the the AI always stays on the ground
         character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
         animation.Play("run");
         }
     }
     
     function OnCollisionEnter(collision : Collision){
         if(collision.gameObject.tag == "Projectile"){
             captured = true;
         }
     }
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 HunterKrech · Mar 01, 2013 at 06:59 AM

     function OnCollisionEnter(collision : Playa){
         if(playa.gameObject.tag == "ground"){ //tag ground
            isGrounded = true; //new bool
         }
          else
           {
                 isGrounded = false;
           }
     }
 function Update()
 {
       if(isGrounded == false)
       {
               transform.Translate = Vector3(0,-1,0)*Time.deltaTime;
       }
 }

Should work alright, pseudo code so dont copy pasta, but something along these line should work

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

9 People are following this question.

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

Related Questions

I'm creating a 3D platformer, but whenever my player goes down a slope, it thinks it isn't grounded, resulting in a weird animation glitch. 1 Answer

No gravity on NPC. Floating above the ground. 2 Answers

adding a gravity system to character controller 1 Answer

[Solved] NPC kinda sorta slides under player 1 Answer

Applying Gravity to AI on chase 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