Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Andar : MonoBehaviour
- {
- // Start is called before the first frame update
- Rigidbody2D rb;
- SpriteRenderer renderer;
- public float speed = 30f;
- bool faceRight = false;
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- renderer = GetComponent<SpriteRenderer>();
- }
- // Update is called once per frame
- void Update()
- {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- Vector3 force = new Vector3(h, v) * speed;
- if (h > 0.1) {
- faceRight = true;
- }
- if (h < 0) {
- faceRight = false;
- }
- renderer.flipX = faceRight;
- rb.AddForce(force);
- rb.velocity = Vector3.ClampMagnitude(rb.velocity, 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement