• 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 CommanderDJ · Jun 21, 2013 at 05:23 AM · movetowards

Alternative to Vector3.MoveTowards()?

Hello,

This is my first post here, I got started with the free version of Unity a few days ago and am really liking it so far!

I'm working to get some simple AI going for my game, and I've made some good progress where the AI follows its target if it can see the target - now, I'm not using waypoints or nodes for my AI at this point (something I'll probably have to look into in the future), just plain transform.position changes. I'm running into a problem where my AI, which is using a CharacterController, doesn't respect the normal collision detection provided by CharacterControllers - it passes through objects that don't have Rigidbodies attached to them. I figure this is because I use Vector3.MoveTowards() for its movement. Here's the function in question:

 function FollowTarget()
 {
     transform.LookAt(targetPosition);
     
     var range = Vector3.Distance(targetPosition.position, transform.position);
     
     //Move towards the target while we're out of range.
     if(range > targetRange)
     {        
         transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, speed * Time.deltaTime);
     }
 }

I tried making this work with CharacterController.Move() so that it respects collision detection, but got strange/inaccurate results with the movement itself. Can anyone suggest an alternative method of doing this?

Comment
Add comment · Show 6
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 robertbu · Jun 21, 2013 at 05:48 AM 0
Share

Untested idea: Could you combine $$anonymous$$oveTowards() and $$anonymous$$ove()?

 if(range > targetRange)
 {   
    var v3$$anonymous$$ove = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition.position, speed * Time.deltaTime);
    v3$$anonymous$$ove -= transform.position;
    controller.$$anonymous$$ove(v3$$anonymous$$ove);
 }



avatar image CommanderDJ · Jun 21, 2013 at 06:40 AM 0
Share

Sadly, this made no difference.

I also realised while testing that the AI's CapsuleCollider didn't actually extend outside its model (it's just a cube for now), so I tried making that bigger, but that made no difference either... and now occasionally the thing falls through the floor :/

But that's a separate issue, :P.

avatar image whydoidoit · Jun 21, 2013 at 07:38 AM 1
Share

So using anything apart from $$anonymous$$ove or Simple$$anonymous$$ove on the character controller will have no collision response at all unless you are using physics and the character's rigid body is non-kinematic (plus you'd probably have to move it in FixedUpdate).

avatar image CommanderDJ · Jun 21, 2013 at 10:00 AM 0
Share

CharacterControllers don't use Rigidbodies, though, do they? $$anonymous$$y understanding was that they were two separate ways of doing physics, and that whilst a CharacterController doesn't automatically have things like gravity (without Simple$$anonymous$$ove(), that is), it should still respect basic collision detection.

avatar image whydoidoit · Jun 21, 2013 at 10:14 AM 0
Share

No they don't have rigidbodies, it's one way or the other. The only way that a CC does collision is if you move it with $$anonymous$$ove or Simple$$anonymous$$ove.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by N-8-D-e-v · Jul 18, 2020 at 10:30 PM

You can use Vector3.Slerp https://docs.unity3d.com/ScriptReference/Vector3.Slerp.html

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 richardgengle · Jul 18, 2020 at 07:51 PM

ive tried forces, like

Vector3 dir2target = (bc.position - transform.position);

// and even slowed it down with magnitude of closeness...
// Vector3 dir2target = (bc.position-transform.position).magnitude/10*(bc.position - transform.position); rb.AddForce(dir2target, ForceMode.Force);

... but for some reason the object just orbits its target bc.position :\

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

MoveTowards doesn't work after changing tag. 2 Answers

How to get click position in Vector 2 ? 1 Answer

Simple GameObjects. Unexpected movement. 0 Answers

Magnet Power Up,moving the coins towards the player 1 Answer

How do I get bullet to move towards mouse when spawned. 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