Advertisement
Guest User

EMBARASSing

a guest
Jun 24th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5.  
  6. public class PlayerController : MonoBehaviour {
  7.  
  8.     int rotNum; //in case I would ever need to know what cardinal direction the player is facing.
  9.     bool canMove = true;
  10.  
  11.     bool currentlyInteracting = false;
  12.     float speed = 5.0f;
  13.     bool isAxisInUse = false;
  14.     Vector3 pos;
  15.     GameObject dialogue;
  16.     GameObject dialogueText;
  17.  
  18.     string[] allLines = File.ReadAllLines(@"C:\Users\Public\Documents\Unity Projects\Dungeon-Crawler\Script.txt");
  19.  
  20.     // Use this for initialization
  21.     void Start () {
  22.         rotNum = 0;
  23.         pos = transform.position;
  24.         //dialogue = GameObject.Find("Dialogue");
  25.         //dialogueText = dialogue.transform.GetChild(3).gameObject;
  26.         //dialogue.SetActive(false);
  27.     }
  28.    
  29.     // Update is called once per frame
  30.     void Update () {
  31.         if (canMove == true) {
  32.  
  33.             //Movement Code
  34.             if (Input.GetAxis("Horizontal") > 0) {
  35.                 if(isAxisInUse == false) {
  36.                     transform.Rotate(0,90,0);
  37.                     if (rotNum == 3) {
  38.                         rotNum = 0;
  39.                     }else {
  40.                         rotNum++;
  41.                     }
  42.                     isAxisInUse = true;
  43.                     Debug.Log("rotNum = " + rotNum);
  44.                 }
  45.             }
  46.             if (Input.GetAxis("Horizontal") < 0) {
  47.                 if (isAxisInUse == false) {
  48.                     transform.Rotate(0,-90,0);
  49.                     if (rotNum == 0) {
  50.                         rotNum = 3;
  51.                     }else {
  52.                         rotNum--;
  53.                     }
  54.                     isAxisInUse = true;
  55.                     Debug.Log("rotNum = " + rotNum);
  56.                 }
  57.             }
  58.             if (Input.GetAxis("Horizontal") == 0) {
  59.                 isAxisInUse = false;
  60.             }
  61.             if (Input.GetKeyDown(KeyCode.W)) {  
  62.                 bool disableMove = false;
  63.                 RaycastHit hit;
  64.  
  65.                 if (Physics.Raycast(transform.position, transform.forward, out hit, 1.0f)) {
  66.                     if(hit.collider.gameObject.tag == "Wall") {
  67.                         disableMove = true;
  68.                         Debug.Log("Hit a Wall");
  69.                     }else {
  70.                         disableMove = false;
  71.                     }
  72.                 }
  73.  
  74.                 if (disableMove == false) {
  75.                     pos += transform.forward;
  76.                 }
  77.  
  78.             }
  79.             if (Input.GetKeyDown(KeyCode.S)) {  
  80.                 bool disableMove = false;
  81.                 RaycastHit hit;
  82.  
  83.                 if (Physics.Raycast(transform.position, (transform.forward * -1), out hit, 1.0f)) {
  84.                     if(hit.collider.gameObject.tag == "Wall") {
  85.                         disableMove = true;
  86.                         Debug.Log("Hit a Wall");
  87.                     }else {
  88.                         disableMove = false;
  89.                     }
  90.                 }
  91.                 if (disableMove == false) {
  92.                     pos += transform.forward * -1;
  93.                 }
  94.             }
  95.             if (Input.GetKeyDown(KeyCode.Q)) {
  96.                 bool disableMove = false;
  97.                 RaycastHit hit;
  98.  
  99.                 if (Physics.Raycast(transform.position, (transform.right * -1), out hit, 1.0f)) {
  100.                     if(hit.collider.gameObject.tag == "Wall") {
  101.                         disableMove = true;
  102.                         Debug.Log("Hit a Wall");
  103.                     }else {
  104.                         disableMove = false;
  105.                     }
  106.                 }
  107.                 if (disableMove == false) {
  108.                     pos += transform.right * -1;
  109.                 }
  110.             }
  111.             if (Input.GetKeyDown(KeyCode.E)) {
  112.                 bool disableMove = false;
  113.                 RaycastHit hit;
  114.  
  115.                 Debug.DrawRay(transform.position, Vector3.forward, Color.green, 1.0f);
  116.                 if (Physics.Raycast(transform.position, transform.right, out hit, 1.0f)) {
  117.                     if(hit.collider.gameObject.tag == "Wall") {
  118.                         disableMove = true;
  119.                         Debug.Log("Hit a Wall");
  120.                     }else {
  121.                         disableMove = false;
  122.                     }
  123.                 }
  124.                 if (disableMove == false) {
  125.                     pos += transform.right;
  126.                 }  
  127.             }
  128.             transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
  129.         }
  130.  
  131.             //Interaction Code
  132.             if (Input.GetKeyDown(KeyCode.Space) && currentlyInteracting == false) {
  133.                 RaycastHit hit;
  134.                 if (Physics.Raycast(transform.position, transform.forward, out hit, 1.0f)) {
  135.                     if(hit.collider.gameObject.tag == "Character") {
  136.                         currentlyInteracting = true;
  137.                         StartDialogue();
  138.                         DialogueLines(allLines, 0, 5);
  139.                         currentlyInteracting = false;
  140.                     }
  141.                 }
  142.             }
  143.     }
  144.  
  145.     void StartDialogue() {
  146.         Debug.Log("Starting Dialogue");
  147.         canMove = false;
  148.         //dialogue.SetActive(true);
  149.     }
  150.  
  151.     void DialogueLines(string[] lines, int startLine, int endLine) {
  152.         Debug.Log("Reading DialogueLines");
  153.         int counter = startLine;
  154.         string curLine = "";
  155.         if (Input.GetKeyDown(KeyCode.Space)) {
  156.             curLine = lines[counter];
  157.             Debug.Log(counter);
  158.             counter++;
  159.         }
  160.         if (counter == endLine) {
  161.             Debug.Log("Ending Dialogue.");
  162.             canMove = true;
  163.             dialogue.SetActive(false);
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement