I'm trying to make a game in the style of many NES games and I'd like object movement to do two things:

  1. I want to move an exact number of pixels per update.

  2. I want, for lack of a better term, absolute movement. The object should move or not move, I don't want a ramp up or down in speed.

I'm trying to do this with physics because I would like to use Unity's hitbox collision detection. Moving objects using the translation property does not allow for this, at least that is my understanding.

Any help is appreciated and if my thought process is off, I'd like any suggestions or pointers in the right direction, thanks.

Here is my move method that gets called on Update. I expect to movement to be in whole pixels, however, when I test the x and y are always fractional.

private void Move()
    VerticalMovement = 0;
    HorizontalMovement = 0;

    if (Input.GetKey(KeyCode.W))
        VerticalMovement = 1;

    if (Input.GetKey(KeyCode.S))
        VerticalMovement = -1;

    if (Input.GetKey(KeyCode.A))
        HorizontalMovement = -1;

    if (Input.GetKey(KeyCode.D))
        HorizontalMovement = 1;

    this.GetComponent<Rigidbody2D>().velocity = new Vector2(HorizontalMovement * Speed, VerticalMovement * Speed);

1 Answer 1


If you just want to "snap" from pixel to pixel, you would probably have to set the position explicitly, like rigidbody.position = new Vector2(x, y). Manipulating the velocity will always yield fractional numbers.

Using a Rigidbody2D, you could probably use MovePosition to achieve something like this as well.

Also, you should put your code inside of FixedUpdate() for anything concerning physics interactions.

  • 1
    \$\begingroup\$ That looks like what I want to do. I was under the impression that I couldn't set an objects coordinates explicitly and use physics collision detection. The documentation for MovePosition says otherwise. I'll give it a try, thanks! \$\endgroup\$
    – lobsterhat
    Mar 16, 2015 at 19:48
  • \$\begingroup\$ Glad to help @lobsterhat. If you think my post answered your question, please mark it "accepted". \$\endgroup\$
    – lase
    Mar 16, 2015 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .