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 /
This question was closed Jan 21, 2012 at 12:43 AM by SrBilyon for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by SrBilyon · Jan 18, 2012 at 04:49 AM · physicsaifloatfollow

Trying to make a Floating Enemy follow player

I'm trying to implement an object that will follow behind my player AND rise and fall as my character jumps and land, drops in descent, etc.

The AI is currently using a character controller as collider and is able to follow the player's transform with an offset, but when the player is standing still or jump and matches the AI object's Y, the AI bounces up and down. I am currently using a character controller and characterController.Move(). He is not being affected by gravity.

Here is my current code:

 function Update()
 {
     //If he isn't flinching, he needs to begin his pursuit
     if (activated)
     {
         //Normal AI follows player when in range and attacks when close enough
         if(activated && aiType == "Normal")
             Movement();
         if(activated && aiType == "Observe")
             LookAt();
     }
     
     //Define his movement....
     var movement =  moveDirection  + Vector3 (0, 0, 0);
     
       //...then move him (order of operation :P)
       controller.Move(movement * (Time.deltaTime));
       //transform.position.y = player.transform.position.y;
       
       if (transform.position.y < player.transform.position.y + 2)
       controller.Move(Vector3(0, player.transform.position.y ,0)* (Time.deltaTime));
       else
       if (transform.position.y > player.transform.position.y + 2)
       controller.Move(Vector3(0,-player.transform.position.y ,0)* (Time.deltaTime));
       
       if (transform.position.y == player.transform.position.y + 2)
       controller.Move(Vector3.zero);
 }
 
 
 function LookAt()
 {
    transform.LookAt(Vector3(player.transform.position.x,0,player.transform.position.z));
    transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);    
 }
 
 function Movement()
 {
     var direction : Vector3  = player.transform.position - this.transform.position; //target aquired
     
     //If something is in my sights
     if(direction.magnitude < awareness)
     {    
         direction.y = 0; 
     } 
 
     if (direction.magnitude > 1f && activated) //if we are away from the wandering point
     {
         Debug.DrawRay(transform.position+Vector3(0,.1,0), transform.forward, Color.black);
         transform.forward = (transform.forward * 0.9f) + (direction * 0.1f);
         controller.Move((transform.forward * maxSpeed ) * Time.deltaTime);
     }
 } 

Is there a more effective way to make this AI and still retain his solidity?

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

  • Sort: 
avatar image
-1

Answer by Hendrys · Jan 18, 2012 at 05:05 AM

Check if your AI character has a Character Controller this kind of component reacts to gravity, so when your AI tries to "reach" the player it inmediately falls down. And it tries to reach the player agaian and falls ans goes on and on. That's the reason of the Bouncing.

Try to elliminate the gravity reaction from your code. And do you really need a character controller? why no trying a regular Collider? doesn't that fit your requirements?

I also think you don't need the last three if's in your Update function. Remove them.

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 SrBilyon · Jan 18, 2012 at 05:09 AM 0
Share

$$anonymous$$y AI component uses a character controller, and no, it's not affected by gravity. A regular controller could work as well.

The last three If's are the only reason why he lifts in the first place.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Planet Physics AI Follower 0 Answers

Unity Best Practices for Physics + AI 1 Answer

Follow Player Via Waypoints 1 Answer

Operator cannot be used with left hand side of type.... 1 Answer

Having a friendly character follow player 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