Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent(typeof(PlayerPhisics))]
- public class PlayerControl : MonoBehaviour {
- // Player Handling
- public float gravity = 20;
- public float speed = 8;
- public float acceleration = 30;
- public float jumptHeight = 12;
- private float currentSpeed;
- private float targetSpeed;
- private Vector2 amountToMove;
- private PlayerPhisics playerPhisics;
- void Start () {
- playerPhisics = GetComponent<PlayerPhisics>();
- }
- void Update () {
- if (playerPhisics.movementStopped)
- {
- targetSpeed = 0;
- currentSpeed = 0;
- }
- //Input
- targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
- currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);
- if (playerPhisics.grounded) {
- amountToMove.y = 0;
- //Jump
- if (Input.GetButtonDown("Jump")) {
- amountToMove.y = jumptHeight;
- }
- }
- amountToMove.x = currentSpeed;
- amountToMove.y -= gravity * Time.deltaTime;
- playerPhisics.Move(amountToMove * Time.deltaTime);
- }
- //Increse it towards target by speed
- private float IncrementTowards(float n, float target, float a) {
- if (n == target) {
- return n;
- } else {
- float dir = Mathf.Sign(target - n);
- n += a * Time.deltaTime * dir;
- return (dir == Mathf.Sign(target - n))? n: target;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement