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
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);
}
}
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?
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.
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! :)
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);
}
}
}
$$anonymous$$ade a infinite loop for me and crashed my unity, But i should have check the code better anyway.
Yeah this will loop (and block) until the Slime is in range of the Target... I don't think it's what they wanted.
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
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!
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/
Your answer
![](https://koobas.hobune.stream/wayback/20220613062452im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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