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 Svejj · Jan 14, 2015 at 09:43 PM · rigidbody

Check if my AI is grounded

I've created my own AI, very simple though but I'm in need of help at the moment.

The problem is that my AI freezes in the air and I know where the problem is in the script but I have not idea how to solve it.

 {
     public float Stop;
     public int MoveSpeed;
     public Transform Target;
     public Transform mytransform;
     private float groundRange = 1.2f;
     public GameObject Character;
     public float distancebetweenobjects;
     // Use this for initialization
     void Start () 
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         Ray vision = new Ray (transform.position, Vector3.forward);
         RaycastHit x;
 
 
 
         float dist = Vector3.Distance(mytransform.position, Target.position);//distance between AI and player
 
         RaycastHit y;
         Ray isgrounded = new Ray (transform.position, Vector3.down);
 
 
         if (Physics.Raycast (isgrounded, out y, groundRange)) //Problem is here, the groundrange
         {
             if(dist <= distancebetweenobjects && dist >= Stop)
             {
                 mytransform.LookAt(Target);
                 mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
                 Character.animation.CrossFade("Armature|SoldierRun");
             }
             else
             {
                 Character.animation.CrossFade("Armature|SoldierIdle");
             }
             if(dist <= Stop)
             {
                 mytransform.LookAt(Target);
                 Character.animation.Play("Armature|SoldierHit");
 
             }
         }
     }
 
     
 }

Right now if the AI gets above 1.2f or whatever I set the groundrange to, he freezes in the air. I saw other questions about giving the AI a rigidbody but that didn't work very well as he just flew past the screen when he started running.

Any ideas?

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
1
Best Answer

Answer by skylem · Jan 14, 2015 at 09:49 PM

theres a couple ways you can do this and it partly depends on how u've setup your character if you were using a CharacterController to move your player this actually stores a value for .isGrounded so u can access ur charactercontroller and check that value, But given that you are raycasting im going to assume you don't have one on your character, below i'll paste a basic raycast designed for ground detection

         //this is our direction/vector that we raycast
         Vector3 downCheck = transform.TransformDirection(Vector3.down * 0.1f);        
         // this is just a copy of our raycast so we can see whats happening in the Scene.
         Debug.DrawRay(transform.position, downCheck, Color.green, 1f, true);
            // this is our actual raycast, it will use the downCheck to see if there is anything //directly beneath our character
    // if there is anything there it will set a boolean to true
    // otherwise it sets it to false
                 if(Physics.Raycast (transform.position, downCheck, 1f)) {
                         somethingdown = true;        
                 } else { somethingdown = false; }

edit -- Falling.

     // then lastly, we check the boolean(True/False) before we apply some falling this setup basically says if nothing is directly below us we will fall.
         if(somethingdown == false) {
               transform.position = new Vector3(transform.position.x, Physics.gravity.y * Time.deltaTime, transform.position.z);
         }
 
 

EDIT -- Revision of Code.

         Vector3 downCheck = transform.TransformDirection(Vector3.down * 0.1f);
         Debug.DrawRay(transform.position, downCheck, Color.green, 1.5f, true);
         /// Note this first Debug is just to see your raycast you can delete it after your sure its working right.
         if(Physics.Raycast (transform.position, downCheck, 1.5f)) 
         {
             
             somethingdown = true;
         } 
         else 
         { 
             somethingdown = false; 
         }
         if (somethingdown == true) 
         {
             if(dist <= distancebetweenobjects && dist >= Stop)
             {
                 mytransform.LookAt(Target);
                 mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
                 Character.animation.CrossFade("Armature|SoldierRun");
             }
             else
             {
                 Character.animation.CrossFade("Armature|SoldierIdle");
             }
             if(dist <= Stop)
             {
                 mytransform.LookAt(Target);
                 Character.animation.Play("Armature|SoldierHit");
                 
             }
         }
         if(somethingdown == false) 
         {
             transform.position += Physics.gravity / 10;
         }
     }
 }

Comment
Add comment · Show 10 · 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 Svejj · Jan 14, 2015 at 09:59 PM 0
Share

I'm sorry, I dont quite follow you here what this code does

avatar image skylem · Jan 14, 2015 at 10:03 PM 0
Share

ill make some more notes above -- Notes added if i missed anything or was unclear on any point let me know and i'll see if i can explain it more clearly.

avatar image Svejj · Jan 14, 2015 at 10:18 PM 0
Share

I'll get back here when I've managed to get it to work

avatar image Svejj · Jan 14, 2015 at 10:30 PM 0
Share

I'm sorry but I cant get it to work

The AI acts the same when put in the code you wrote into my script except the falling part as he just flys(Dont know the word) up in the air and stops there.

         Vector3 downCheck = transform.TransformDirection(Vector3.down * 0.1f);
         Debug.DrawRay(transform.position, downCheck, Color.green, 1.5f, true);
         /// Note this first Debug is just to see your raycast you can delete it after your sure its working right.
         if(Physics.Raycast (transform.position, downCheck, 1.5f)) 
         {
 
             somethingdown = true;
         } 
         else 
         { 
             somethingdown = false; 
         }
         if (somethingdown == true) 
         {
             if(dist <= distancebetweenobjects && dist >= Stop)
             {
                 mytransform.LookAt(Target);
                 mytransform.position += mytransform.forward * $$anonymous$$oveSpeed * Time.deltaTime;
                 Character.animation.CrossFade("Armature|SoldierRun");
             }
             else
             {
                 Character.animation.CrossFade("Armature|SoldierIdle");
             }
             if(dist <= Stop)
             {
                 mytransform.LookAt(Target);
                 Character.animation.Play("Armature|SoldierHit");
                 
             }
         }
         if(somethingdown == false) 
         {
             transform.position = new Vector3(transform.position.x, Physics.gravity.y * Time.deltaTime, transform.position.z);
         }
avatar image Svejj · Jan 14, 2015 at 10:41 PM 0
Share

Would it be easier if I used a charactercontroller ins$$anonymous$$d of raycasting?

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Refer to a parent Rigidbody 1 Answer

How to use AddForce on FPC 0 Answers

Change player movement 0 Answers

Add force which throws back an object 3 Answers

Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle) 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