bekovski

sg_p7

Jun 27th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. using UnityEngine.UI;
  5.  
  6. public class RunnerControl : MonoBehaviour {
  7.  
  8.     public bool running = false;
  9.     public Vector3 direction = new Vector3(0, 0, 1);
  10.     float speed = 6;
  11.     float maxSpeed = 6;
  12.  
  13.     float maxJumpingHeight = 5;
  14.  
  15.     public float maxJumpTime = 10;
  16.     float airTime;
  17.  
  18.     float rotation;
  19.  
  20.     public Text startGameText;
  21.  
  22.     float touchPosDeltaX;
  23.     float touchPosDeltaY;
  24.     float accelX;
  25.  
  26.     // Use this for initialization
  27.     void Start () {
  28.  
  29.     }
  30.    
  31.     // Update is called once per frame
  32.     void Update () {
  33.    
  34.         // start game
  35.         if(!running && InputSimulation.touchCount > 0) {
  36.             running = true;
  37.             ViewManager.instance.SetText ("");
  38.         }
  39.  
  40.         if (running) {
  41.  
  42.             transform.rotation = Quaternion.AngleAxis(rotation, Vector3.up);
  43.  
  44.             checkInput();
  45.  
  46.             processOrientation();
  47.  
  48.             freezePosition();
  49.  
  50.             if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed) {
  51.                 GetComponent<Rigidbody>().AddForce(direction * speed);
  52.             }
  53.  
  54.             if (transform.position.y == maxJumpingHeight && airTime > 0) {
  55.                 airTime -= Time.deltaTime;
  56.             }
  57.  
  58.             if (transform.position.y == maxJumpingHeight && airTime < 0) {
  59.                 transform.position = new Vector3(transform.position.x, 1, transform.position.z);
  60.             }
  61.  
  62.             checkDeath();
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * verarbeitet die Touch-Eingaben
  68.      */
  69.     void processTouches(){
  70.         // TODO:
  71.         // - create variables for touch positions and more (?)
  72.         if(InputSimulation.touchCount > 0) {
  73.             touchPosDeltaX = InputSimulation.touches [1].deltaPosition.x;
  74.             touchPosDeltaY = InputSimulation.touches [1].deltaPosition.y;
  75.             accelX = InputSimulation.acceleration.x;
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * checks the orientation of the smartphone and moves the runner according to the actual orientation
  81.      */
  82.     void processOrientation() {
  83.         Rigidbody rb = GetComponent<Rigidbody> ();
  84.  
  85.         // TODO problem: when swiping, position always reupdated...
  86.  
  87.         // Debug.Log ("Accel.x = " + InputSimulation.acceleration.x);
  88.         if(InputSimulation.acceleration.x < -0.5f) {
  89.             // transform.position = new Vector3(-1f, transform.position.y, transform.position.z);
  90.             rb.MovePosition (transform.position + new Vector3(-1,0,0) * Time.fixedDeltaTime);
  91.             Debug.Log ("rb = " + rb.position);
  92.         }
  93.         else if(InputSimulation.acceleration.x > 0.5f) {
  94.             // transform.position = new Vector3(1f, transform.position.y, transform.position.z);
  95.             rb.MovePosition (transform.position + Vector3.right * Time.fixedDeltaTime);
  96.         }
  97.         else {
  98.             // transform.position = new Vector3(0f, transform.position.y, transform.position.z);
  99.         }
  100.     }
  101.  
  102.     //sagt, ob ein entsprechender swipe vom Spieler ausgeführt wurde.
  103.     bool swipeLeft(){
  104.         if(InputSimulation.touchCount == 1) {
  105.             SimulatedTouch touch = InputSimulation.GetTouch (0);
  106.             Debug.Log (touch.deltaPosition);
  107.             if(touch.deltaPosition.x < -70) {
  108.                 Debug.Log ("turn left");
  109.                 return true;
  110.             }
  111.             // return touch.deltaPosition.x < -25;
  112.         }
  113.  
  114.         return false;
  115.     }
  116.  
  117.     bool swipeRight(){
  118.         if(InputSimulation.touchCount == 1) {
  119.             SimulatedTouch touch = InputSimulation.GetTouch (0);
  120.             Debug.Log (touch.deltaPosition);
  121.             if(touch.deltaPosition.x > 70) {        // maybe check for something like > 1
  122.                 Debug.Log ("turn right");
  123.                 return true;
  124.             }
  125.             // return touch.deltaPosition.x > 25;
  126.         }
  127.  
  128.         return false;
  129.     }
  130.  
  131.     bool swipeUp(){
  132.         if(InputSimulation.touchCount == 1) {
  133.             SimulatedTouch touch = InputSimulation.GetTouch (0);
  134.             Debug.Log (touch.deltaPosition);
  135.             if(touch.deltaPosition.y > 20) {
  136.                 Debug.Log ("jump up");
  137.                 return true;
  138.             }
  139.             // return touch.deltaPosition.x > 10;
  140.         }
  141.  
  142.         return false;
  143.     }
  144.  
  145.     //Input interpretieren
  146.     void checkInput(){
  147.  
  148.         processTouches();
  149.  
  150.         if (swipeLeft() && CameraMove.instance.GetDegreesRotatedSince () > -90f) {
  151.             direction = Quaternion.AngleAxis(-90, Vector3.up) * direction;
  152.             rotation -= 90;
  153.             float upwardsMovement = GetComponent<Rigidbody>().velocity.y;
  154.             GetComponent<Rigidbody>().velocity = Vector3.zero;
  155.             GetComponent<Rigidbody>().velocity += new Vector3(0, upwardsMovement, 0);
  156.  
  157.             CameraMove.instance.OrbitAroundY (-90f);
  158.  
  159.         }
  160.  
  161.         if (swipeRight() && CameraMove.instance.GetDegreesRotatedSince () < 90f){
  162.             direction = Quaternion.AngleAxis(+90, Vector3.up) * direction;
  163.             rotation += 90;
  164.             float upwardsMovement = GetComponent<Rigidbody>().velocity.y;
  165.             GetComponent<Rigidbody>().velocity = Vector3.zero;
  166.             GetComponent<Rigidbody>().velocity += new Vector3(0, upwardsMovement, 0);
  167.  
  168.             CameraMove.instance.OrbitAroundY (90f);
  169.         }
  170.  
  171.         if (swipeUp()) {
  172.             if (transform.position.y < 2) {
  173.                 transform.position = new Vector3(transform.position.x, maxJumpingHeight, transform.position.z);
  174.             }
  175.         }
  176.     }
  177.    
  178.  
  179.     void die(){
  180.         GetComponent<Rigidbody>().velocity = Vector3.zero;
  181.         direction = new Vector3(0, 0, 1);
  182.         rotation = 0;
  183.         transform.position = new Vector3(0, 1, 0);
  184.         running = false;
  185.  
  186.         CameraMove.instance.ResetCamera ();
  187.     }
  188.  
  189.  
  190.     void checkDeath() {
  191.         if (transform.position.y < 0.0f) {
  192.             die ();
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * make sure the player runs only in one direction.
  198.      */
  199.     void freezePosition(){
  200.         if (direction == new Vector3(0, 0, 1) || direction == new Vector3(0, 0, -1)) {
  201.             GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX;
  202.         } else {
  203.             GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ;
  204.         }
  205.         GetComponent<Rigidbody>().constraints |= RigidbodyConstraints.FreezeRotation;
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment