The question is answered, right answer was accepted
Rigidbody.MovePosition doesn't look smooth
My plan is that a gameobject with a rigidbody attached to it should follow a target object.
In my script the gameobject does follow the target but it doesn't look smooth.
Is there any other way to make a rigidbody smoothly follow a gameobject? I tried using Vector3.MoveTowards and it looks better but the object doesn't collide with other objects. Thats not what I need.
private void FixedUpdate()
{
if (isCarrying == true)
{
Vector3 target = followTarget.transform.position;
currentItem.GetComponent<Rigidbody>().MovePosition(target);
}
}
There are many ways, but physics is hard.
To clarify, is you're object set up like this?
GameObject 1 ($$anonymous$$er) -- No RigidBody -- Parrent of GameObject 2
GameObject 2 (Follower) -- Has RigidBody -- Child of GameObject 1.
GameObject 1, the target, has no Rigidbody. GameObject 2 has a Rigidbody but isn't a child of GameObject 1. They're two separate objects.
Ok, that helps, now second question.
When you say follow. Do you mean "lag behind?" Or stick with it exactly.
Answer by troopy28 · May 25, 2017 at 12:41 PM
Hello, maybe I didn't understand what you want but what about transform.Translate ?
The collider would still work.
[SerializeField]
private Transform target;
public float speed = 0.2f;
private void FixedUpdate()
{
transform.Translate((target.position - transform.position) * Time.deltaTime * speed);
}
Otherwise, have you the interpolation enabled for your rigidbdy? According to the documentation, it is important in order to have smooth moves.
EDIT
AS SpaceManDan said, with the code above, there could be strange results with the physics (mainly during collisions). If you want to make the movement of your rigidbody with a nice collision handling, then you could use something like this :
Vector3 direction = target.position - transform.position;
rigidBody.velocity = ((target.position - transform.position) * Time.deltaTime * speed);
But this is only one of the many ways of impact the velocity of the rigidbody. I recommend you to take a look at there. And do not forget to enable the interpolation on your rigidbody.
Regards,
You sure the collider will still work with a translate? That doesn't sound right. I suppose if it has a rigidbody it will but it wont be true collisions. You should move the rigidbody ins$$anonymous$$d with $$anonymous$$oveToward or $$anonymous$$ovePosition if you decide not to use force to move it. That is, if you intend for true physics collisions. Otherwise, if you use translate you will get inconsistent collisions depending on the distance of the translate. I.$$anonymous$$ you may move the object into a wall and it will snap back out all in the same frame.
Yes you are right, there could be strange results, but here he is only doing a simple follow system. And that's only one of the two solutions I was suggesting, as the other was to simply enable the interpolation and keep the physics code. It would be smoother like this. I will edit my answer to fit to what you said, I hadn't this in $$anonymous$$d.
Like I just mentioned in my comment above. Careful with this method. It's generally bad practice to move rigidbodies with translates unless you are certain where it translates too is going to be collision free.
Have a look at this http://answers.unity3d.com/questions/215377/transformtranslate-vs-rigidbodymoveposition.html
If you use translate, when you start running into problems with it popping in and out of walls, let me know and I'll help you move it with physics.
The main difference, when considering how a RigidBody work in relation to Unity's physic engine is this:
Transform.Translate will happen before the physics engines kicks in and won't take any part of the physics engine into account. No gravity, no drag, etc. During the same frame, when the physic engine does it things, it will consider the change created by Translate as something that has happened in the frame prior. It will basically like an insane amount of force was forced in 1 single frame at light speed. (It's an exaggeration, but it gives an idea). Think of it as if you were to simultaneously stop all gravity, movement air resistance and everything in time, then move the object a bit, then resume all at once. The change in position you applied to the object is then taken into account by all the physics which consider its position from A to B to be quite fast. Because of this, if you ever use Transform.Translate on a Rigidbody, you're usually forced to change one value related to physics by a ton such as applying 0.001f units worth of force or raising the friction or drag by a ton to counter the light speed movement calculated by the physical engine.
RigidBody.$$anonymous$$ovePosition works in a similar way as to Transform.Translate, but in this instance, the movement is done by the physic engine itself during the physic calculation phase. Due to this, it allows Unity's Physic engine to ignore the physics while moving the game object. In other words, the time isn't "stopped" when the object move, but gravity, drag, etc. are simply ignoring the change. A bit like a time travel ins$$anonymous$$d of time stopping.
Changing the RigidBody.Velocity works by moving the game object through the physics engine, but with, in consideration, the drag and friction as well as the rotation if applicable. It's a constant value similar to Gravity such as if the game object was attracted by a $$anonymous$$agnet positioned in the angled based by the Vector3 of the Velocity.
Using RigidBody.AddForce works by instantly adding a force to the Rigidbody. Unlike Velocity which is a constant effect (meaning you can't add 2 Velocities, but ins$$anonymous$$d double the velocity), it's accumulative meaning that using AddForce multiple time result in a movement multiplied by the amount of time it was called. While Velocity will push the object in a constant way, AddForce will actually have a kind of damping in its beginning because, initially, the inertia of the gameobject and its mass, drag, etc. will affect the impact of AddForce a lot more than by changing the Velocity.
Both AddForce and Velocity can works, but the best one depends on what you're ai$$anonymous$$g for. For example, vehicle with multiple rockets can use Addforce on each rocket to move the main body for a physic-like propulsion while the Velocity can be used to simulate the pulling from the planets around.
Answer by Bill9009 · May 24, 2017 at 06:56 PM
try:
currentItem.GetComponent<Rigidbody>().AddForce(target - gameObject.transform.position, ForceMode.Acceleration);
Doesn't work, now it isn't even moving.
Still thanks for your reply!
Do you want the object that is carried to move exactly with the object that carries it? If so then just make the carried object a child of the carrying object.
carryingObject.parent = currentItem.transform;
I had it that way earlier. The problem is that I need the objects rigidbody to e.g keep it from going through walls. Im not sure if Im doing something wrong but if I parent the carried object to the target, the rigidbody doesn't seem to work. Its collision at least.
Follow this Question
Related Questions
Rigid Body and Collision 1 Answer
My Enemies walk through objects and my FPS character model 3 Answers
How to Make A Character Stop At Wall? 0 Answers
Stopping Moving Objects? 0 Answers