Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player : MonoBehaviour {
  5.     public float maxSpeed = 3;
  6.     public float Speed = 50f;
  7.     public float jumppower = 150f;
  8.     public bool grounded;
  9.    
  10.     private Rigidbody2D rb2d;
  11.     private Animator anim;
  12.    
  13.    
  14.     // Use this for initialization
  15.     void Start ()
  16.     {
  17.         rb2d = gameObject.GetComponent<Rigidbody2D>();
  18.         anim = gameObject.GetComponent<Animator>();
  19.     }
  20.    
  21.     // Update is called once per frame
  22.     void Update ()
  23.     {
  24.         anim.SetBool("Grounded", grounded);
  25.         anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
  26.        
  27.         if (Input.GetAxis ("Horizontal") < -0.1f) {
  28.             transform.localScale = new Vector3(-1, 1, 1);
  29.         }
  30.         if (Input.GetAxis ("Horizontal") > 0.1f) {
  31.             transform.localScale = new Vector3(1, 1, 1);
  32.         }
  33.     }
  34.    
  35.     void FixedUpdate()
  36.     {
  37.         float h = Input.GetAxis("Horizontal");
  38.        
  39.         //Move player
  40.         rb2d.AddForce((Vector2.right * Speed) * h);
  41.        
  42.         //speed limiting
  43.         if (rb2d.velocity.x > maxSpeed) {
  44.             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
  45.         }
  46.         if (rb2d.velocity.x < -maxSpeed) {
  47.         }
  48.         rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
  49.     }
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement