/* * Script par Valentin Bloch * */ using UnityEngine; public class PlayerMovement : MonoBehaviour { public float speed; private Rigidbody2D myRigidbody; private Vector3 change; // Start is called before the first frame update void Start() { myRigidbody = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { change = Vector3.zero; change.x = Input.GetAxisRaw("Horizontal"); change.y = Input.GetAxisRaw("Vertical"); if (change != Vector3.zero) { MoveCharacter(); } Vector3 pos = Camera.main.WorldToViewportPoint(transform.position); pos.x = Mathf.Clamp01(pos.x); pos.y = Mathf.Clamp01(pos.y); transform.position = Camera.main.ViewportToWorldPoint(pos); } void MoveCharacter() { myRigidbody.MovePosition(transform.position + change * speed * Time.deltaTime); } }