using UnityEngine;
using System.Collections;
[RequireComponent(typeof(playerphysics))]
public class PlayerController : MonoBehaviour {
// Player Handling
public float speed = 8;
public float acceleration = 12;
private float currentSpeed;
private float targetSpeed;
private Vector2 amountToMove;
private playerphysics playerphysics;
void Start () {
playerphysics = GetComponents<playerphysics> ();
}
void Update () {
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementsTowards (currentSpeed, targetSpeed,acceleration);
amountToMove = new Vector2 (currentSpeed, 0);
playerphysics.Move(amountToMove * Time.deltaTime);
}
// Increase n towards target by speed
private float IncrementsTowards(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;
}
}
}