Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AsAnAsterisk · Jun 19, 2017 at 02:11 AM · rigidbodyaienemy airigidbody.addforceenemyai

How to move enemy with physics based movement?

Fairly new to Unity/C#. I'm doing an FPS. I've gotten all the animations down, but when it comes to actually having the enemy move, I'm having some issues. I'd like a physics based locomotion so the creature doesn't just clip through walls. I'm aware of Rigidbody,AddForce and CharacterController.Move but I have no idea how to "phrase" them given my current setup or what values to put where.

I've tried the AddForce page, but again, I'm unsure how to phrase it in a way, so that when the Walking animation starts, and the enemy has spotted the player, that it moves toward them.

 public class WastrelChase : MonoBehaviour {
 
     public Transform player;
     static Animator anim;
     
     
 
     // Use this for initialization
     void Start ()
     {
         anim = GetComponent<Animator>();
         
     }
     
     // Update is called once per frame
     void Update () {
 
         
         if (Vector3.Distance(player.position, this.transform.position) < 5)
         {
             Vector3 direction = player.position - this.transform.position;
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                         Quaternion.LookRotation(direction), 0.1f);
 
             anim.SetBool("IsIdle", false);
             if (direction.magnitude > .8)
             {
                 anim.SetBool("IsWalking", true);
                 anim.SetBool("IsStriking", false);
             }
             else
             {
                 anim.SetBool("IsStriking", true);
                 anim.SetBool("IsWalking", false);
             }
 
 
         }
         else
         {
             anim.SetBool("IsIdle", true);
             anim.SetBool("IsWalking", false);
             anim.SetBool("IsStriking", false);
         }
     }
 }
 


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

Answer by bobisgod234 · Jun 19, 2017 at 03:22 AM

Is there a reason your anim variable is static? This means that all instances of "WastrelChase" will share the same Animator component, which surely won't end well. I would recommend removing the static. keyword and replacing it with private, so it becomes

      private Animator anim;
  

Back to the question, you need a reference to your Rigidbody. You can add it to the top of the script and assign it just like your anim component:

      private Rigidbody myRigidbody;
     
      // Use this for initialization
      void Start ()
      {
          myRigidbody = GetComponent<Rigidbody >();
      }

Next, make sure your object has some collider attached to it.

Depending on your enemy type, it may be a good idea to lock the X and Z rotation of the Rigidbody component, to prevent it from rolling around.

Next is to apply the force to the rigidbody. AddForce should be used in the FixedUpdate function, so what we will do is create a new Vector3 variable in our MonoBehaviour called "moveDirection", which we will use in the Fixed update function.

The top of your class should now look something like:

      public Transform player;
      private Animator anim;
      private Rigidbody myRigidbody;
      private Vector3 moveDirection;

Set it to be zero at the very start of your Update function:

      void Update () {
  
          moveDirection = Vector3.zero;
 
          if (Vector3.Distance(player.position, this.transform.position) < 5)
          {

And assign it the direction you wish your object to move in, where you set IsWalking to true:

              if (direction.magnitude > .8)
              {
                  moveDirection = direction.normalized;
                  anim.SetBool("IsWalking", true);
                  anim.SetBool("IsStriking", false);
              }

Then, add your FixedUpdate function:

 void FixedUpdate()
 {
     myRigidbody.AddForce(myDirection * 50f);
 }

the 50f is the movement force. You will need to adjust it to get movement working right. If it doesn't appear to be moving, try increasing this to very large numbers (e.g. 50000f) to see if its just due to the force being to low.

The finished script will look something like:

  public class WastrelChase : MonoBehaviour {
  
      public Transform player;
      private Animator anim;
      private Rigidbody myRigidbody;
      private Vector3 moveDirection;
  
      // Use this for initialization
      void Start ()
      {
          anim = GetComponent<Animator>();
          myRigidbody = GetComponent<Rigidbody>();
      }
      
      // Update is called once per frame
      void Update () {
  
          moveDirection = Vector3.zero;
 
          if (Vector3.Distance(player.position, this.transform.position) < 5)
          {
              Vector3 direction = player.position - this.transform.position;
              direction.y = 0;
  
              this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                          Quaternion.LookRotation(direction), 0.1f);
  
              anim.SetBool("IsIdle", false);
              if (direction.magnitude > .8)
              {
                  moveDirection = direction.normalized;
                  anim.SetBool("IsWalking", true);
                  anim.SetBool("IsStriking", false);
              }
              else
              {
                  anim.SetBool("IsStriking", true);
                  anim.SetBool("IsWalking", false);
              }
  
  
          }
          else
          {
              anim.SetBool("IsIdle", true);
              anim.SetBool("IsWalking", false);
              anim.SetBool("IsStriking", false);
          }
      }
 
     void FixedUpdate()
     {
         myRigidbody.AddForce(moveDirection * 50f);
     }
  }

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 AsAnAsterisk · Jun 19, 2017 at 06:27 PM 0
Share

thanks for your help, but the enemy is still shuffling in place, even with the force at 500000, is there something i should set my rigidbody / collider as in Unity? it doesn't appear to be affected by gravity either.

avatar image AsAnAsterisk · Jun 19, 2017 at 10:58 PM 0
Share

never$$anonymous$$d actually! turns out i just had to turn off kinematic for rigidbody in unity! well, looks like i can't use a mesh collider but oh well, not too big of a deal, thanks for your help!

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

122 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 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 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 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 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

Enemy Rigidbody Not Responding to Gravity 1 Answer

Why do the AI get caught on trees? 0 Answers

Simple AI Error 8025 Parrsing error. 2 Answers

AI passes through walls and flies 2 Answers

My Eneny AI Script Wont Work? 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