Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class car : MonoBehaviour {
- private float speed = 0f;
- private float maxSpeed = 15f;
- private float startAngluraDrag;
- private float startOrthoSize;
- void Start () {
- startOrthoSize = Camera.main.orthographicSize;
- startAngluraDrag = GetComponent<Rigidbody2D> ().angularDrag;
- }
- void FixedUpdate () {
- CameraFunctions ();
- InputHandlingKB ();
- MovementHandlersRigid ();
- }
- //using rigidbody2d
- private void MovementHandlersRigid() {
- GetComponent<Rigidbody2D> ().AddForce (transform.right * speed);
- if (GetComponent<Rigidbody2D> ().velocity.magnitude > 0.5f) {
- GetComponent<Rigidbody2D> ().angularDrag = startAngluraDrag;
- GetComponent<Rigidbody2D> ().AddTorque (-Input.GetAxisRaw ("Horizontal"));
- } else {
- GetComponent<Rigidbody2D> ().angularDrag = 10f;
- }
- }
- //keyboard && gamepad
- private void InputHandlingKB() {
- if (Input.GetKey (KeyCode.UpArrow) || Input.GetAxisRaw ("Fire2") > 0f) {
- if (speed < maxSpeed) {
- speed += 0.1f;
- }
- } else if (Input.GetKey (KeyCode.DownArrow) || Input.GetAxisRaw ("Fire3") > 0f) {
- if (speed > 0f) {
- speed -= 0.3f;
- }
- } else {
- if (speed > 0f) {
- speed -= 0.2f;
- }
- }
- if (speed < 0f) {
- speed = 0f;
- }
- }
- //camera follow, smooth
- private void CameraFunctions() {
- if (speed > startOrthoSize) {
- Camera.main.orthographicSize = Mathf.Lerp (Camera.main.orthographicSize, 5f, Time.deltaTime * 0.5f);
- } else {
- Camera.main.orthographicSize = Mathf.Lerp (Camera.main.orthographicSize, startOrthoSize, Time.deltaTime * 0.5f);
- }
- Camera.main.transform.position -= (Camera.main.transform.position - new Vector3(transform.position.x, transform.position.y, -10f)) * Time.deltaTime * 10f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement