- Home /
Handling collision with fast Lerp
I have a game, where the player is an object that is Lerped to the position underneath the mouse. You must dodge oncoming objects.
Problem is, when you move your mouse outside the screen in the top, and then back inside in the bottom, the player will almost instantly Lerp to the bottom, completely ignoring collisions with any objects inbetween the top point where you exited, and the bottom point where you entered.
Do I have any options here, or do I have to hard-code/hack some stupid raycast check before Lerping?
EDIT: I should probably mention, that collision is handled with a rigidbody on my object, as well as on all obstacles. Obstacles are triggers, player is not.
How are you moving the object? You need to do it in physics with $$anonymous$$ovePosition.
void $$anonymous$$oveTowards (Vector3 pos) {
Vector3 newPos = new Vector3(pos.x, transform.position.y, pos.z);
transform.position = Vector3.Lerp(transform.position, newPos, moveSpeed * Time.deltaTime);
}
I am moving the object like this, with the input Vector3 pos being the raycast-located mouse position in world space.
I want the movement to be fluid ie. lerping, so the object is slightly lagging behind the mouse. Does $$anonymous$$ovePosition accomplish this?
Answer by Mattyizz · Feb 14, 2014 at 05:08 PM
Actually, this fixed the problem:
void MoveTowards (Vector3 pos) {
Vector3 newPos = new Vector3(pos.x, transform.position.y, pos.z);
rigidbody.position = Vector3.Lerp(transform.position, newPos, moveSpeed * Time.deltaTime);
}
Calling the function on the position of the rigidbody instead of the transform.
You're a champ, thanks mate.
Hopefully that will work on all platforms, you used to have to use $$anonymous$$ovePosition on the rigidbody to ensure that the physics properly interacted, but it looks like just moving the rigidbody will do it now...
Your answer
Follow this Question
Related Questions
Can I have a single rigidbody2d that acts as both collider and trigger? 1 Answer
Objects don't collide 1 Answer
How would I go about setting an exclusive collision, i.e. floor that only reacts to the player? 1 Answer
Is there a better way to check for a collision with a prefab than by name or tag? 2 Answers
Collision & 2D 2 Answers