using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarController : MonoBehaviour {

	public float speed = 1500f;
	public float rotationSpeed = 15f;

	public WheelJoint2D LeftWheel;
	public WheelJoint2D RightWheel;

	public Rigidbody2D rb;

	private float movement = 0f;
	private float rotation = 0f;

	void Update() 
	{
		movement = -Input.GetAxisRaw ("Vertical") * speed;
		rotation = Input.GetAxisRaw ("Horizontal");

	}

	void FixedUpdate() 
	{
		if (movement == 0f) 
		{
			LeftWheel.useMotor = false;
			RightWheel.useMotor = false;
		} else 
		{
			LeftWheel.useMotor = true;
			RightWheel.useMotor = true;

			JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = LeftWheel.motor.maxMotorTorque };
			LeftWheel.motor = motor;
			RightWheel.motor = motor;
		}

		rb.AddTorque (-rotation * rotationSpeed * Time.fixedDeltaTime);
	}

}
