- Home /
Question by
unity_rGIukRg4_7lKww · Aug 12, 2020 at 09:22 AM ·
c#movementprogrammingmovement script2d-physics
How to make the player move a fixed distance? (Tile per tile)
I want to make my player move one tile each time I press the button on keyboard, without teleporting and smoothily. Just exactly like the game Tibia. In my code I did it, but the player moves too fast from one tile to another, and I don't know how to make it slower, i'm quite new to programming.
public class PlayerMovement : MonoBehaviour { float sqm = 0.16f; public float moveSpeed; public Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); // Moves the GameObject using it's transform. rb.isKinematic = true; } private void FixedUpdate() { if(Input.GetKeyDown(KeyCode.W)){ rb.MovePosition(transform.position + transform.up * moveSpeed * sqm * Time.deltaTime); } }
Comment