Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody2D))]
- public class CatController : MonoBehaviour {
- public float moveSpeed;
- public float runSpeed;
- Rigidbody2D rb;
- Vector2 moveVector;
- bool isRunning;
- private void Start() {
- rb = GetComponent<Rigidbody2D>();
- }
- private void Update() {
- float x = Input.GetAxisRaw("Horizontal"); // -1, 0, 1
- float y = Input.GetAxisRaw("Vertical"); // -1, 0, 1
- moveVector = new Vector2(x, y).normalized;
- isRunning = Input.GetKey(KeyCode.LeftShift);
- if (x > 0) {
- transform.localEulerAngles = new Vector3(0, 0, 0);
- } else if (x < 0) {
- transform.localEulerAngles = new Vector3(0, 180, 0);
- }
- }
- private void FixedUpdate() { // 0.02s
- if (isRunning) {
- rb.velocity = moveVector * runSpeed;
- } else {
- rb.velocity = moveVector * moveSpeed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement