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 nkcowmaster · Apr 09, 2018 at 01:26 AM · airigidbody2dvelocityvector2

Vector2.MoveTowards and Rigidbody2D Velocity

I am scripting an AI Enemy that is supposed to move toward and retreat from the player based on distance ranges. This is working fine, however when I expand the Info on the Rigidbody of the AI, the X Velocity is stuck at 0.

This is an issue because I am trying to flip the AI Enemy when its X velocity is in the negative (e.g. movement > 0.0f)

Here is my script for how the AI moves towards the player, and the script for flipping the sprite.

     //Simple AI Behaviors
     if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
     {
         transform.position = Vector2.MoveTowards(transform.position, player.position, movementSpeed * Time.deltaTime);
     }
     else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
     {
         transform.position = this.transform.position;
     }
     else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
     {
         transform.position = Vector2.MoveTowards(transform.position, player.position, -movementSpeed * Time.deltaTime);
     }

     movement = GetComponent<Rigidbody2D>().velocity.x * movementSpeed;

     //Flip Player Sprite
     if (movement > 0.0f)
     {
         transform.rotation = Quaternion.Euler(0, 180, 0);
     }
     else if (movement < 0.0f)
     {
         transform.rotation = Quaternion.identity;
         Debug.Log("SpiritAnimal");
     }

My assumption is that Vector2.MoveTowards is not sending X Velocity information to the Rigidbody of the AI Enemy... how could I re-write my pursuit code to utilize Rigidbody Velocity?

Thank you.

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

Answer by bobisgod234 · Apr 09, 2018 at 05:28 AM

Setting transform.position is like teleporting the object around, it will look like smooth movement, but to the physics engine, there is no velocity.

Try this, but put it at the top of your script, before you assign to transform.position:

      movement = player.position.x - transform.position.x;
 

Also, line 8 doesn't actually do anything, you just assign a value to itself, since transform and this.transform are the same thing.

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 nkcowmaster · Apr 10, 2018 at 03:23 AM 0
Share

That got it working, thank you!

I notice that the velocity is still not being reported, though I suppose that is due to the $$anonymous$$oveForward not reporting velocity like you mentioned. I wonder if there is a way to do this and still apply velocity...

One odd thing that is going on is my Enemy AI gets super jittery as it moves, but when it hits the ceiling of the level (the AI flys like a flappy bird), it smoothens itself out perfectly, only to repeat the cycle over and over again. Any thoughts? Otherwise, thank you for solving my original issue!

avatar image
0

Answer by ItWillBeEpic · Apr 09, 2018 at 04:58 AM

Have you tried flipping enemy entirely instead of just flipping its sprite?

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 nkcowmaster · Apr 10, 2018 at 03:24 AM 0
Share

Yes, actually that is what I am doing. Apologize for my mistyping!

avatar image
0

Answer by nkcowmaster · Apr 10, 2018 at 03:26 AM

@Bobisgod234 had the answer I was looking for, thank you all for your help!

Bob, if you can toss a reply in the comment section I can Accept your answer as the correct one. I don't see the option for your original reply.

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 bobisgod234 · Apr 10, 2018 at 04:01 AM 0
Share

I converted my comment to an answer, so you can accept it.

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

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

Related Questions

Object jitters when the scene starts 0 Answers

How can I tweak acceleration and deceleration of a Rigidbody2D with .AddForce()? 0 Answers

Rigbody 2D Velocity Mystery 1 Answer

How do I Control An Objects Movement Only On the X Axis and Have a RigidBody Control Movement on the Y axis? 1 Answer

How do I add forward velocity to a Rigidbody2D? 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