Advertisement
Guest User

Unity

a guest
Aug 28th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1.         [SerializeField]
  2.         private float speed;
  3.  
  4.         [SerializeField]
  5.         private float jumpHeight;
  6.  
  7.         [SerializeField]
  8.         private float gravity;
  9.  
  10.         [SerializeField] private Vector3 direction;
  11.  
  12.         private new Rigidbody2D rigidbody2D;
  13.  
  14.         private void Awake()
  15.         {
  16.             rigidbody2D = GetComponent<Rigidbody2D>();
  17.         }
  18.  
  19.         private void Update()
  20.         {
  21.             if (!OnGround())
  22.             {
  23.                 direction.y -= gravity * Time.deltaTime;
  24.                 return;
  25.             }
  26.  
  27.             if (Input.GetKeyDown(KeyCode.Space))
  28.                 direction.y = jumpHeight;
  29.  
  30.             direction = new Vector3(Input.GetAxis("Horizontal") * speed, direction.y, direction.z);
  31.         }
  32.  
  33.         private void FixedUpdate()
  34.         {
  35.             rigidbody2D.velocity = direction;
  36.         }
  37.  
  38.         private bool OnGround()
  39.         {
  40.             return Physics2D.Raycast(transform.position, Vector2.down, 1, LayerMask.GetMask("Floor"));
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement