Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class RunnerControl : MonoBehaviour {
- public bool running = false;
- public Vector3 direction = new Vector3(0, 0, 1);
- float speed = 6;
- float maxSpeed = 6;
- float maxJumpingHeight = 5;
- public float maxJumpTime = 10;
- float airTime;
- float rotation;
- public Text startGameText;
- float touchPosDeltaX;
- float touchPosDeltaY;
- float accelX;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- // start game
- if(!running && InputSimulation.touchCount > 0) {
- running = true;
- ViewManager.instance.SetText ("");
- }
- if (running) {
- transform.rotation = Quaternion.AngleAxis(rotation, Vector3.up);
- checkInput();
- processOrientation();
- freezePosition();
- if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed) {
- GetComponent<Rigidbody>().AddForce(direction * speed);
- }
- if (transform.position.y == maxJumpingHeight && airTime > 0) {
- airTime -= Time.deltaTime;
- }
- if (transform.position.y == maxJumpingHeight && airTime < 0) {
- transform.position = new Vector3(transform.position.x, 1, transform.position.z);
- }
- checkDeath();
- }
- }
- /**
- * verarbeitet die Touch-Eingaben
- */
- void processTouches(){
- // TODO:
- // - create variables for touch positions and more (?)
- if(InputSimulation.touchCount > 0) {
- touchPosDeltaX = InputSimulation.touches [1].deltaPosition.x;
- touchPosDeltaY = InputSimulation.touches [1].deltaPosition.y;
- accelX = InputSimulation.acceleration.x;
- }
- }
- /**
- * checks the orientation of the smartphone and moves the runner according to the actual orientation
- */
- void processOrientation() {
- Rigidbody rb = GetComponent<Rigidbody> ();
- // TODO problem: when swiping, position always reupdated...
- // Debug.Log ("Accel.x = " + InputSimulation.acceleration.x);
- if(InputSimulation.acceleration.x < -0.5f) {
- // transform.position = new Vector3(-1f, transform.position.y, transform.position.z);
- rb.MovePosition (transform.position + new Vector3(-1,0,0) * Time.fixedDeltaTime);
- Debug.Log ("rb = " + rb.position);
- }
- else if(InputSimulation.acceleration.x > 0.5f) {
- // transform.position = new Vector3(1f, transform.position.y, transform.position.z);
- rb.MovePosition (transform.position + Vector3.right * Time.fixedDeltaTime);
- }
- else {
- // transform.position = new Vector3(0f, transform.position.y, transform.position.z);
- }
- }
- //sagt, ob ein entsprechender swipe vom Spieler ausgeführt wurde.
- bool swipeLeft(){
- if(InputSimulation.touchCount == 1) {
- SimulatedTouch touch = InputSimulation.GetTouch (0);
- Debug.Log (touch.deltaPosition);
- if(touch.deltaPosition.x < -70) {
- Debug.Log ("turn left");
- return true;
- }
- // return touch.deltaPosition.x < -25;
- }
- return false;
- }
- bool swipeRight(){
- if(InputSimulation.touchCount == 1) {
- SimulatedTouch touch = InputSimulation.GetTouch (0);
- Debug.Log (touch.deltaPosition);
- if(touch.deltaPosition.x > 70) { // maybe check for something like > 1
- Debug.Log ("turn right");
- return true;
- }
- // return touch.deltaPosition.x > 25;
- }
- return false;
- }
- bool swipeUp(){
- if(InputSimulation.touchCount == 1) {
- SimulatedTouch touch = InputSimulation.GetTouch (0);
- Debug.Log (touch.deltaPosition);
- if(touch.deltaPosition.y > 20) {
- Debug.Log ("jump up");
- return true;
- }
- // return touch.deltaPosition.x > 10;
- }
- return false;
- }
- //Input interpretieren
- void checkInput(){
- processTouches();
- if (swipeLeft() && CameraMove.instance.GetDegreesRotatedSince () > -90f) {
- direction = Quaternion.AngleAxis(-90, Vector3.up) * direction;
- rotation -= 90;
- float upwardsMovement = GetComponent<Rigidbody>().velocity.y;
- GetComponent<Rigidbody>().velocity = Vector3.zero;
- GetComponent<Rigidbody>().velocity += new Vector3(0, upwardsMovement, 0);
- CameraMove.instance.OrbitAroundY (-90f);
- }
- if (swipeRight() && CameraMove.instance.GetDegreesRotatedSince () < 90f){
- direction = Quaternion.AngleAxis(+90, Vector3.up) * direction;
- rotation += 90;
- float upwardsMovement = GetComponent<Rigidbody>().velocity.y;
- GetComponent<Rigidbody>().velocity = Vector3.zero;
- GetComponent<Rigidbody>().velocity += new Vector3(0, upwardsMovement, 0);
- CameraMove.instance.OrbitAroundY (90f);
- }
- if (swipeUp()) {
- if (transform.position.y < 2) {
- transform.position = new Vector3(transform.position.x, maxJumpingHeight, transform.position.z);
- }
- }
- }
- void die(){
- GetComponent<Rigidbody>().velocity = Vector3.zero;
- direction = new Vector3(0, 0, 1);
- rotation = 0;
- transform.position = new Vector3(0, 1, 0);
- running = false;
- CameraMove.instance.ResetCamera ();
- }
- void checkDeath() {
- if (transform.position.y < 0.0f) {
- die ();
- }
- }
- /**
- * make sure the player runs only in one direction.
- */
- void freezePosition(){
- if (direction == new Vector3(0, 0, 1) || direction == new Vector3(0, 0, -1)) {
- GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX;
- } else {
- GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ;
- }
- GetComponent<Rigidbody>().constraints |= RigidbodyConstraints.FreezeRotation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment