- Home /
Using Rigidbody.MovePosition With Local Position and Lerp
I simply want to change this function to use Rigidbody.MovePosition instead of local transform position, but so far I can't figure out how to convert the local positions to something that works with that method. I've read the existing discussions on the topic, but none of them seem to apply to my particular situation. I know this must be pretty simple to do, right?
// target here is a local offset position
private IEnumerator MoveToPosition (Vector3 target)
{
float t = 0;
Vector3 start = transform.localPosition;
while (t <= 1)
{
t += Time.fixedDeltaTime / m_Speed;
transform.localPosition = Vector3.Lerp (start, target, t);
yield return null;
}
}
Answer by IsaiahKelly · Apr 16, 2018 at 12:10 AM
Okay, I was really overthinking this. I just needed to get (world) transform.position instead of transform.localPosition and feed that into the Rigidbody.MovePosition. Just make sure target value is also in world coordinates.
private IEnumerator MoveToPosition (Vector3 target)
{
float t = 0;
Vector3 start = transform.position;
while (t <= 1)
{
t += Time.fixedDeltaTime / m_Speed;
Rigidbody.MovePosition (Vector3.Lerp (start, target, t));
yield return null;
}
}
Answer by waikit97 · Apr 13, 2018 at 03:09 AM
private float m_Speed = 10;
public GameObject targetObj; //the target object
public Rigidbody m_rb; //your moving object's RigidBody
private IEnumerator MoveToPosition (GameObject target)
{
float t = 0;
while (t <= 1)
{
t += Time.fixedDeltaTime / m_Speed;
Vector3 start = transform.localPosition; //assign inside the while loop for updating your moving object's local position, since it moves
m_rb.MovePosition(start + target.transform.localPosition * Time.deltaTime);
yield return null;
}
}
Thanks, but this doesn't seem to give me the desired effect and isn't using Vector3.Lerp It also didn't seem to work correctly when I tried it in my own code.
Your answer
Follow this Question
Related Questions
Is mixing movement types a bad thing to do? (Force and Translate) 1 Answer
Simple Movement Game: Physics vs Manual Collision Detection? 2 Answers
Kinematics: Trying to get an object to land at a known point 1 Answer
Moving a kinematic object a fixed distance over a set time with physics in mind 2 Answers
Infinite Moving/Rotating Question 1 Answer