Newer
Older
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Clinton Galbraith
committed
public class PlayerBehavior : MonoBehaviour {
/* Movement to apply this frame */
private Vector3 frameMovement;
[SerializeField]
private float acceleration;
[SerializeField]
private float brakeFraction;
Clinton Galbraith
committed
[SerializeField]
private float maxSpeed;
Clinton Galbraith
committed
public GameObject player;
private Rigidbody2D playerBody;
public void MoveLeft()
{
frameMovement.x -= acceleration;
Clinton Galbraith
committed
}
public void MoveRight()
{
frameMovement.x += acceleration;
Clinton Galbraith
committed
}
public void MoveUp()
{
frameMovement.y += acceleration;
Clinton Galbraith
committed
}
public void MoveDown()
{
frameMovement.y -= acceleration;
}
public void Brake()
{
playerBody.velocity *= brakeFraction;
Clinton Galbraith
committed
}
// Use this for initialization
void Start () {
playerBody = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
Clinton Galbraith
committed
void Update () {
playerBody.AddForce(Vector3.ClampMagnitude(frameMovement, acceleration));
if(playerBody.velocity.magnitude > maxSpeed)
{
playerBody.velocity = (playerBody.velocity.normalized * maxSpeed);
}
frameMovement = Vector3.zero;
}
}