Skip to content

Instantly share code, notes, and snippets.

@sydney4529
Created April 9, 2018 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sydney4529/6dc9e0fadf4fbe2403cf12b18cb3afea to your computer and use it in GitHub Desktop.
Save sydney4529/6dc9e0fadf4fbe2403cf12b18cb3afea to your computer and use it in GitHub Desktop.
Code for the player controller interactivity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerCollision : MonoBehaviour
{
//Tags for each individual object
private bool photoPlay = false;
private bool mugPlay = false;
private bool phonePlay = false;
private bool shellPlay = false;
private bool braceletPlay = false;
private bool anchorPlay = false;
private bool cassettePlay = false;
private bool winePlay = false;
private bool stickyPlay = false;
public static int counter = 0;
private int playCounter = 0;
private int animCounter = 0;
private string voice;
private string party;
public GameObject pe;
public GameObject pe2;
Animator anim;
Animator anim2;
private bool open = false;
private bool open2 = false;
void Start()
{
pe = GameObject.Find("NTOpen2Ani");
anim = pe.GetComponent<Animator>();
pe2 = GameObject.Find("DeskOpen1Ani");
anim2 = pe2.GetComponent<Animator>();
}
void Update() {
//Casts physics ray from the FPS controller
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//if the raycast hits an object
if (Physics.Raycast(ray, out hit))
{
if (Input.GetButtonDown("AButton") || Input.GetMouseButtonDown(0)){
if (hit.collider.tag == "Book" && animCounter == 0)
{
if (open == true) {
open = false;
//animcounter makes it so that only one animation will play at a time
animCounter++;
voice = "Drawer";
PlayVoiceLine();
//sets the animation trigger to the closing drawer animation
anim.SetTrigger("Back");
//sets it so that you can only play one animation at a time
Invoke("animPlay", 4);
}
else
{
open = true;
animCounter++;
voice = "Drawer";
PlayVoiceLine();
anim.SetTrigger("Active");
//sets it so that you can only play one animation at a time
Invoke("animPlay", 4);
}
}
}
if (Input.GetButtonDown("AButton") || Input.GetMouseButtonDown(0))
{
if (hit.collider.tag == "Desk" && animCounter == 0)
{
if (open2 == true)
{
open2 = false;
playCounter++;
voice = "Drawer";
PlayVoiceLine();
anim2.SetTrigger("Back2");
Invoke("playing", 4);
}
else
{
playCounter++;
open2 = true;
voice = "Drawer";
PlayVoiceLine();
anim2.SetTrigger("Active1");
Invoke("playing", 4);
}
}
}
//particle effect code, sets them to change when the raycast hits the correct tag for each particle and object.
if (hit.collider.tag == "Select")
{
ChangeParticleColor("PMug", aColor);
party = "PMug";
}
else if (hit.collider.tag == "Bracelet")
{
ChangeParticleColor("PBracelet", Color.blue);
party = "PBracelet";
}
else if (hit.collider.tag == "Cassette")
{
ChangeParticleColor("PCassette", Color.blue);
party = "PCassette";
}
else if (hit.collider.tag == "Wine")
{
ChangeParticleColor("PWine", Color.blue);
party = "PWine";
}
else if (hit.collider.tag == "Phone")
{
ChangeParticleColor("PPhone", Color.blue);
party = "PPhone";
}
else if (hit.collider.tag == "Shell")
{
ChangeParticleColor("PShell", Color.blue);
party = "PShell";
}
else if (hit.collider.tag == "Anchor")
{
ChangeParticleColor("PAnchor", Color.blue);
party = "PAnchor";
}
else if (hit.collider.tag == "Sticky")
{
ChangeParticleColor("PSticky", Color.blue);
party = "PSticky";
}
else if (hit.collider.tag == "Photo")
{
ChangeParticleColor("PPhoto", Color.blue);
party = "PPhoto";
}
else
{
//if the gaze is not on an object, set its colour back to white
ChangeParticleColor(party, Color.white);
}
}
//both mouse and controller input compatible
if (Input.GetButtonDown("AButton") || Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(ray, out hit)){
if (hit.collider.tag == "Select" && mugPlay == false && playCounter == 0)
{
// adds one to play counter, making it impossible to play voicelines over each other
playCounter++;
//sets which voiceline for the object
voice = "Mug";
PlayVoiceLine();
//Mug can only be played once
mugPlay = true;
//adds to the voiceline counter so once 4 objects have been clicked on, new voice lines play after these ones finish
Invoke("Counter", 6);
//makes the game check if the voicelines are still playing
Invoke("playing", 8);
//makes the particle effect dissapear after being clicked on
pe = GameObject.Find("PMug");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Bracelet" && braceletPlay == false && playCounter == 0)
{
playCounter++;
voice = "Bracelet";
PlayVoiceLine();
braceletPlay = true;
Invoke("Counter", 17);
Invoke("playing", 19);
pe = GameObject.Find("PBracelet");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Cassette" && cassettePlay == false && playCounter == 0)
{
playCounter++;
voice = "Cassette";
PlayVoiceLine();
cassettePlay = true;
Invoke("Counter", 20);
Invoke("playing", 22);
pe = GameObject.Find("PCassette");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Phone" && phonePlay == false && playCounter == 0)
{
playCounter++;
voice = "Phone";
PlayVoiceLine();
phonePlay = true;
Invoke("Counter", 45);
Invoke("playing", 47);
pe = GameObject.Find("PPhone");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Shell" && shellPlay == false && playCounter == 0)
{
playCounter++;
voice = "Shell";
PlayVoiceLine();
shellPlay = true;
Invoke("Counter", 26);
Invoke("playing", 28);
pe = GameObject.Find("PShell");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Anchor" && anchorPlay == false && playCounter == 0)
{
playCounter++;
voice = "Anchor";
PlayVoiceLine();
anchorPlay = true;
Invoke("Counter", 18);
Invoke("playing", 20);
pe = GameObject.Find("PAnchor");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Wine" && winePlay == false && playCounter == 0)
{
playCounter++;
voice = "Wine";
PlayVoiceLine();
winePlay = true;
Invoke("Counter", 28);
Invoke("playing", 30);
pe = GameObject.Find("PWine");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Sticky" && stickyPlay == false && playCounter == 0)
{
playCounter++;
voice = "Sticky";
PlayVoiceLine();
stickyPlay = true;
Invoke("Counter", 11);
Invoke("playing", 13);
pe = GameObject.Find("PSticky");
pe.GetComponent<ParticleSystem>().Stop();
}
if (hit.collider.tag == "Photo" && photoPlay == false && playCounter == 0)
{
playCounter++;
voice = "Photo";
PlayVoiceLine();
photoPlay = true;
Invoke("Counter", 18);
Invoke("playing", 20);
pe = GameObject.Find("PPhoto");
pe.GetComponent<ParticleSystem>().Stop();
}
}
}
}
//function for changing particle effects, finds the correct particle effect game object and references its start color to be changed
void ChangeParticleColor(string Go, Color Pcolor)
{
pe = GameObject.Find(Go);
pe.GetComponent<ParticleSystem>().startColor = Pcolor;
}
//before the final line plays, the music is set to stop, this function is called
void stopMusic()
{
FindObjectOfType<SAudioManager>().Stop("Theme");
}
//refernces the audio manager code to play the voiceline audio
void PlayVoiceLine()
{
FindObjectOfType<SAudioManager>().Play(voice);
}
//counter for the voicelines that play after objects are clicked on
void Counter()
{
counter++;
if (counter == 4)
{
//adds to the counter to make sure objects can't be clicked while these play
playCounter++;
voice = "Mean1";
//gives the previous line a second before this one plays
Invoke("PlayVoiceLine", 1);
Invoke("playing", 10);
}
if (counter == 5)
{
playCounter++;
voice = "Mean2";
Invoke("PlayVoiceLine", 1);
Invoke("playing", 9);
}
if (counter == 6)
{
playCounter++;
voice = "Mean3";
Invoke("PlayVoiceLine", 1);
Invoke("playing", 7);
}
if (counter == 7)
{
playCounter++;
voice = "Mean4";
Invoke("PlayVoiceLine", 1);
Invoke("playing", 9);
}
if (counter == 8)
{
playCounter++;
voice = "Mean5";
Invoke("PlayVoiceLine", 1);
Invoke("playing", 19);
}
if (counter == 9)
{
playCounter++;
voice = "Mean6";
Invoke("PlayVoiceLine", 1);
Invoke("playing", 15);
//sets the restart menu to play a couple seconds after everything is finished playing
Invoke("restartMenu", 37);
//final line plays after the previous line
Invoke("finalLine", 20);
//music stops just before final line
Invoke("stopMusic", 19);
}
}
//final voiceline refernce
void finalLine()
{
FindObjectOfType<SAudioManager>().Play("Final1");
}
//triggers the scene change to the restart menu
void restartMenu()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
//function to subtract from the counter to allow the lines the ability to play again
void playing()
{
playCounter--;
}
//function to subtract from the counter to allow the animations the ability to play again
void animPlay()
{
animCounter--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment