- Home /
Lerping on rigidbody2d.position isn't working properly, is it supposed to?
Hi guys, I have a 2D game where I need grid-based movement behavior. I have written this code, and it seem to work just fine when I used transform.position. However that didn't register collisions, so I tried rigidbody2d.MovePosition, that resulted in a continuous movement and was not what I wanted. So I scraped that and decided to lerp the rigidbody position, problem is that it just sometimws works and sometimes it doesn't. The code executes till the end without errors but the behavior says otherwise. Here's the code.
public class Tile : MonoBehaviour
{
public float itemScore;
public float smooth = 2;
private bool moving = false;
void FixedUpdate()
{
Control ();
}
void Control ()
{
//If the tile isn't moving, take the player input and store it in a 2D vector. If that vector
//is not zero, start the movement coroutine.
if(!moving)
{
Vector2 input = new Vector2 (Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"));
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
input.y = 0;
} else
{
input.x = 0;
}
if(input != Vector2.zero)
{
StartCoroutine(Move (input));
}
}
}
private IEnumerator Move (Vector2 input)
{
float t = 0;
moving = true;
Vector2 startPos = rigidbody2D.position;
Vector2 targetDelta = new Vector2 (input.x * GameController.GM.distanceDelta,input.y * GameController.GM.distanceDelta);
Vector2 targetPos = startPos + targetDelta;
while(rigidbody2D.position != targetPos)
{
t += Time.deltaTime * smooth;
rigidbody2D.position = Vector2.Lerp(startPos,targetPos,t);
yield return null;
}
moving = false;
yield return null;
}
So any tips?
Answer by Sausagesauce · Sep 29, 2014 at 09:39 PM
I'd recommend not using Vector2.Lerp for this. Use Vector2.MoveTowards instead. Example:
//Speed per frame for movement
var Step : float;
//Moving?
var MoveToPos : boolean;
//Target
var target;
function Update(){
if(MoveToPos){
//If you are moving, move towards position. Define target on click.
transform.position = Vector2.MoveTowards(transform.position, target.position, Step);
}
}
isnt movetowards the same as lerp? Difference is that movetowards is limiting the speed. Also you can't call vector2 functions on a transform.position since its of vector3 type. I can't get it to work but I think that even if I did this would not register collisions.
Answer by Anxo · Sep 29, 2014 at 10:08 PM
The hole point of a rigid body is to let physics control the motion, you never really want to tell a rigidbody where to be positioned but if you do, you would tell the transform, not the rigidbody. In case you wish to move the rigidbody to register collisions and not go through things, you either add force or if you need to control it more as your questions states (grid based) , you can use velocity.
while(transform.position != targetPos)
{
rigidbody2d.velocity = transform.position - targetPos;
yield return new WaitForEndOfFrame();
}
Unchecked code may bug out but you get the idea, apply any smoothing you like.