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 Sehyo · Jan 17, 2013 at 08:46 PM · aihowgroundto

Keep AI @ right height, walk down hills and up and stuff.

Hi, I'm writing a simple AI script that follows waypoints and can chase the player. I'm wondering how to keep the AI at right height / on the ground, cos right now it flies around :P I don't want it to have any problems going down a hill or up one (unless its too steep of course)...Could anyone tutor me on how to do this? Here is the code at the moment:

 using UnityEngine;
 using System.Collections;
 public class blablabla : MonoBehaviour
 {
     static float speed = 60;
     public Transform[] waypoint;
     public AudioClip[] feukaSounds;
     float distanceToPlayer;
     int currentWaypoint;
     public float feukaTimer = 3.0f;
     enum SeekerState
     {
         seeking,
         walkingToNext,
         playerSpotted,
         stuck
     };
     SeekerState seekerState = SeekerState.walkingToNext;
     void Update()
     {
         switch(seekerState)
         {
             case SeekerState.seeking:
             seek();
             break;
             case SeekerState.walkingToNext:
             walkToNext();
             break;
             case SeekerState.playerSpotted:
             chase();
             break;
             case SeekerState.stuck:
             unstuck();
             break;
         }
         transform.rotation = Quaternion.identity;
     }
     void seek()
     {
         
     }
     void walkToNext()
     {
         if(currentWaypoint < waypoint.Length)
         {
             Vector3 target = waypoint[currentWaypoint].position;
             Vector3 moveDirection = target - transform.position;
             Vector3 velocity = rigidbody.velocity;
             if(moveDirection.magnitude < 1)
                 currentWaypoint++;
             else
             {
                 double direction = Mathf.Atan2(moveDirection.x,moveDirection.z);
                 moveDirection.x += Mathf.Sin((float)direction);
                 moveDirection.z += Mathf.Cos((float)direction);
                 velocity = moveDirection.normalized*speed;
             }
             rigidbody.velocity = velocity;
         }
         else
             currentWaypoint = 0;
         if(Vector3.Distance(GameObject.Find("Player").transform.position, gameObject.transform.position) < 100f)
             seekerState = SeekerState.playerSpotted;
     }
     void chase()
     {
         if(feukaTimer <= 0)
         {
             audio.PlayOneShot(feukaSounds[Random.Range(0,feukaSounds.Length)], 100f);
             feukaTimer = Random.Range(5, 15);
         }
         else
             feukaTimer -= Time.deltaTime;
         if(Vector3.Distance(GameObject.Find("Player").transform.position, gameObject.transform.position) < 300f)
         {
             Vector3 target = GameObject.Find("Player").transform.position;
             Vector3 moveDirection = target - transform.position;
             Vector3 velocity = rigidbody.velocity;
             if(moveDirection.magnitude < 1)
             {
                 // Kill player
             }
             else
             {
                 double direction = Mathf.Atan2(moveDirection.x,moveDirection.z);
                 moveDirection.x += Mathf.Sin((float)direction);
                 moveDirection.z += Mathf.Cos((float)direction);
                 velocity = moveDirection.normalized*speed;
             }
             rigidbody.velocity = velocity;
             
         }
         
     }
     void unstuck()
     {
         
     }
 }
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ryba · Jan 17, 2013 at 09:01 PM

Since you seem to be using rigidbody for movement physics, just make sure you have checked "use gravity" in rigidbody component in inspector. While testing to make sure for 100% you are using gravity you can add such line at beggining of Update in your AI component:

 rigidbody.useGravity = true;

If your monster still "flies", try making them heavier, by increasing rigidbody mass (inspector or in code: rigidbody.mass = 5;)

If enemies still fly in straight line between two points, you could try to use character controller for them instead.

And now most important thing. Make sure u have "isKinematic" set to false (kinematic rigidbodies doesn't use dynamic physics, like newtons laws).

For beeing sure at 100% that you are not using "isKinematic", add at begining of Update line:

 rigidbody.isKinematic = false;
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 Sehyo · Jan 17, 2013 at 10:56 PM

@Ryba, The gravity is enabled, kinematic is off and I increased the mass. My "monster" still flies T_T

Thanks tho :P

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 ryba · Jan 18, 2013 at 05:46 AM 0
Share

Did you tried using CharacterController ins$$anonymous$$d of rigidbody ?

avatar image
0

Answer by Imankit · Jan 18, 2013 at 08:10 AM

Apply this script to all your waypoints...

 using UnityEngine;
 using System.Collections;
 
 public class waypointPosition : MonoBehaviour {
     RaycastHit hit;
     void Start() {
         if(Physics.Raycast(transform.position, -Vector3.up, out hit)) {
             transform.position = hit.point + new Vector3(0, 0.15f, 0);        
         }
     }
     
 }


This will keep all your waypoints to ground.. May be your waypoints are up in the sky.... and the target direction is calculated according to that

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

10 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

Related Questions

AI Enemies? 1 Answer

how to lock screen? 1 Answer

How to kill enemies by jumping on their heads? 1 Answer

Can't Understand GetComponent C# 2 Answers

How to make a third person free raom? 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