Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Andar : MonoBehaviour
  6. {
  7.     // Start is called before the first frame update
  8.     Rigidbody2D rb;
  9.     SpriteRenderer renderer;
  10.     public float speed = 30f;
  11.  
  12.     bool faceRight = false;
  13.     void Start()
  14.     {
  15.         rb = GetComponent<Rigidbody2D>();
  16.         renderer = GetComponent<SpriteRenderer>();
  17.     }
  18.  
  19.     // Update is called once per frame
  20.     void Update()
  21.     {
  22.         float h = Input.GetAxis("Horizontal");
  23.         float v = Input.GetAxis("Vertical");
  24.         Vector3 force = new Vector3(h, v) * speed;
  25.  
  26.         if (h > 0.1) {
  27.             faceRight = true;
  28.         }
  29.  
  30.         if (h < 0) {
  31.             faceRight = false;
  32.         }
  33.         renderer.flipX = faceRight;
  34.  
  35.         rb.AddForce(force);
  36.  
  37.         rb.velocity = Vector3.ClampMagnitude(rb.velocity, 4);
  38.        
  39.        
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement