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 /
  • Help Room /
avatar image
0
Question by tatelax · Dec 29, 2011 at 05:06 AM · ai

How would I Move a rigidbody towards another object?

I am developing an AI.

Before, I did not have a rigidbody attached to my object so I just said to use transform. This will no longer work because I need to use a rigidbody now.

THANKS

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

Answer by asafsitner · Dec 29, 2011 at 09:48 AM

I suppose the first thing you'll want to do is rotate the rigidbody towards the target, then propel it forward. That, or if you don't want it to face the target you can simply determine the vector towards the target and propel the rigidbody along that vector.

Example for implementing the first method(all code is c#):

 void FollowTargetWithRotation(Transform target, float distanceToStop, float speed)
     {
         if(Vector3.Distance(transform.position, target.position) > distanceToStop)
         {
             transform.LookAt(target);
             rigidbody.AddRelativeForce(Vector3.forward * speed, ForceMode.Force);
         }
     }

An example of implementing the second method:

 void FollowTargetWitouthRotation(Transform target, float distanceToStop, float speed)
     {
         var direction = Vector3.zero;
         if(Vector3.Distance(transform.position, target.position) > distanceToStop)
         {
             direction = target.position - transform.position;
             rigidbody.AddRelativeForce(direction.normalized * speed, ForceMode.Force);
         }
     }
Comment
Add comment · Show 3 · 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 adrianov · Mar 20, 2016 at 06:40 PM 0
Share

When I try this, my game gets stuck in the while loop EVERY single time. I don't understand why. Checked to see if for some reason the target position was unreachable, causing the loop to always be true, but that doesn't seem to be the problem. Anyone know why this might be happening?

avatar image Eno-Khaon adrianov · Mar 20, 2016 at 08:54 PM 1
Share

Well, it's not terribly surprising that you would wind up stuck in a loop using that script as-is.

Rather than "while" loops, those should really just be "if" statements.

Because the movement is not applied on that specific frame, but after it has ended, all that happens ins$$anonymous$$d is an endless addition of velocity, but that velocity never gets the chance to be applied as a position modification.

avatar image asafsitner Eno-Khaon · Mar 21, 2016 at 07:21 PM 0
Share

Good observation. At the time this was meant more as a pseudo-code example, but I've updated it with your feedback in case anyone else copies it verbatim. Thanks! :)

avatar image
-1

Answer by MrSpuriz · Mar 05, 2016 at 04:11 PM

Anyone using this code today, use this one instead: using UnityEngine; using System.Collections;

 public class SlimeMovement : MonoBehaviour {
 
     Rigidbody2D rb;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody2D>();
     }
     
     void FollowTargetWithRotation(Transform target, float distanceToStop, float speed)
     {
         while(Vector3.Distance(transform.position, target.position) > distanceToStop)
         {
             transform.LookAt(target);
             rb.AddRelativeForce(Vector3.forward * speed, ForceMode.Force);
         }
     }
 }
Comment
Add comment · Show 4 · 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 timothy92 · Aug 07, 2016 at 03:08 AM 0
Share

$$anonymous$$ade a infinite loop for me and crashed my unity, But i should have check the code better anyway.

avatar image JohnBubriski · Aug 10, 2016 at 07:13 PM 0
Share

Yeah this will loop (and block) until the Slime is in range of the Target... I don't think it's what they wanted.

avatar image MrSpuriz · Aug 10, 2016 at 07:28 PM 0
Share

it was not my intention to crash your game guys, this code worked for me when i used it, sorry if it didn't worked, maybe it was the unity version... Anyways, sorry if it crashed your game

avatar image JohnBubriski MrSpuriz · Aug 11, 2016 at 08:01 PM 0
Share

The problem isn't even that it crashes the game... I don't think it does what the asker wants. I'm guessing he wants to move a object towards another object over time. In your script force will be applied until the object is within range all within the same Update frame. This will "block" until the operation succeeds.

To the asker's point though, I don't quite understand how this would even work! I think that the force would actually get applied during FixedUpdate()... which means the object never moves in this loop, which means it will never get there and the game will hang!

avatar image
0

Answer by unity_iyL2QG3JOPsS4w · Jul 14, 2020 at 03:11 AM

This is what i found: https://forum.unity.com/threads/from-mathf-movetowards-to-rigidbody-moveposition.349882/

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

how to search for a random point until the condition is true? 0 Answers

How to make enemy chase player. Basic AI 7 Answers

enemy ai walking randomly and stopping 0 Answers

My sprite looks like it is haveing a seizure. What am I doing wrong? 1 Answer

Advanced AI sight? AI can detect you if part of you is visible to them. Is it Possible? 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