This website uses cookies to keep track of the traffic to this website. If you want to keep using this website, then you must accept our policy: More information/policy
I don’t see myself as a coder but it is something I enjoy. These are the C# scripts used to program my game Firecrackers cheese adventure. It’s for the unity engine and works very well. The only thing missing here is the camera script. I used the standard third-person camera rig from the unity engine. And unfortunately, the camera is the worst part of the game. I only had a few weeks while doing a special effects course to do this project so I’m happy it worked out as well as it did.
A simple script that makes the main menu usable and easy to update.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class main_menu_script : MonoBehaviour
{
// get game objects
public GameObject gameLogo;
public GameObject gameName;
public GameObject mainMenu;
public GameObject lvlSelectGroup1;
public GameObject howToPlay;
//buttons
public Button backButton;
public Button lvlSelectButton;
public Button howToPlayButton;
public Button quitButton;
//buttons
//lvl select buttons
public Button betaLvl;
public Button lvl1_button;
public Button lvl2_button;
public Button lvl3_button;
//lvl select buttons
// Start is called before the first frame update
void Start()
{
backButton.onClick.AddListener(backToMain);
lvlSelectButton.onClick.AddListener(lvlSelect);
quitButton.onClick.AddListener(quitGame);
howToPlayButton.onClick.AddListener(htpbClick);
//lvl_selects (This is going to be changed to a for each loop at a later stage! to get all lvls!)
betaLvl.onClick.AddListener(runBetaLvl);
lvl1_button.onClick.AddListener(runLvl1);
lvl2_button.onClick.AddListener(runLvl2);
lvl3_button.onClick.AddListener(runLvl3);
//lvl_selects
Cursor.visible = true;
}
void backToMain()
{
SceneManager.LoadScene("main_menu");
}
void lvlSelect()
{
mainMenu.gameObject.SetActive(false);
lvlSelectGroup1.gameObject.SetActive(true);
backButton.gameObject.SetActive(true);
}
void quitGame()
{
Application.Quit();
}
void runBetaLvl()
{
SceneManager.LoadScene("SampleScene");
}
void runLvl1()
{
SceneManager.LoadScene("level1");
}
void runLvl2()
{
SceneManager.LoadScene("level2");
}
void runLvl3()
{
SceneManager.LoadScene("level3");
}
void htpbClick()
{
howToPlay.gameObject.SetActive(true);
backButton.gameObject.SetActive(true);
}
}
This script handles the rotation of various pickups in the game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pickup_rotation : MonoBehaviour
{
public Transform pickupObject;
public float rotateSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
pickupObject.Rotate(new Vector3(0, 15, 0) * Time.deltaTime * rotateSpeed);
}
}
This script handles how the player interacts with the environment and objects. It also handles how the UI updates and measures time and points.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class player_interaction : MonoBehaviour
{
//scripts
public player_movement playerMScript;
//components
public GameObject InteractionsSoundObject;
public GameObject allPickups;
AudioSource interactionSoundSource;
Collider InteractionColider;
// ui screens
public GameObject blackScreen;
public GameObject pauseScreen;
public GameObject deadScreen;
public GameObject winScreen;
public GameObject utilityButtons;
public Button replayButton;
public Button mainMenuButton;
public Button quitGameButton;
public Text winText;
public Text winTimeText;
// ui screens
//fulegage
public Image p10;
public Image p20;
public Image p30;
public Image p40;
public Image p50;
public Image p60;
public Image p70;
public Image p80;
public Image p90;
public Image p100;
public Image unlimitedFuleImage;
public Image unlimitedFuleImage2;
public Image OutOfFuleImage;
public Text unlimFuleText;
public Text scoreText;
public Text timeText;
//fulegage
//sounds
public AudioClip[] cheeseDoodleClips;
public AudioClip[] cheeseBallClips;
public AudioClip[] DoritoClips;
AudioClip PickUpSound;
//floats
public float cheeseDoodlefule = 1;
public float cheeseBallfule = 10;
public float maxFule;
public float fule = 1000f;
public float fuleDrainRate = 5f;
public float score;
public float cheeseDoodlePoints = 1;
public float cheeseBallPoints = 10;
public float doritoPoints = 100;
public float unlimitedFuleTime = 10f;
float millisecondsV;
float milliseconds;
float seconds;
//fule calculations=============================================
float fulePerc;
float unlimFuleTimer;
float timeDrain_Unlim;
//fule calculations=============================================
//bools
public bool outOfFule;
bool unlimitedFule;
bool pauseScreenActive;
bool allowedToPause;
bool allreadyDead;
//ints
int rangeScan;
int toPlay;
int pickupInt;
int minutes;
// String
string timerString;
// Start is called before the first frame update
void Start()
{
interactionSoundSource = InteractionsSoundObject.gameObject.GetComponent();
maxFule = fule;
unlimitedFule = false;
unlimitedFuleImage.gameObject.SetActive(false);
unlimitedFuleImage2.gameObject.SetActive(false);
OutOfFuleImage.gameObject.SetActive(false);
turnBlackscreenOff();
unlimFuleTimer = 0;
scoreText.text = "0";
minutes = 0;
pauseScreenActive = false;
allowedToPause = true;
allreadyDead = false;
//in game menu buttons (ex. for pause menu)
replayButton.onClick.AddListener(restartLvl);
mainMenuButton.onClick.AddListener(mainMenu);
quitGameButton.onClick.AddListener(quitGame);
//in game menu buttons (ex. for pause menu)
//cursor disable
Cursor.visible = false;
}
private void Awake()
{
getAllPickups();
}
private void getAllPickups()
{
ParticleSystem[] pickparticles = allPickups.gameObject.GetComponentsInChildren();
foreach (ParticleSystem p in pickparticles)
{
p.Stop();
}
}
// Update is called once per frame
void Update()
{
//Timer
seconds = Time.timeSinceLevelLoad % 60;
//devides seconds in to minutes.
minutes = (int)Time.timeSinceLevelLoad / 60;
//Gets milliseconds from seconds
millisecondsV = seconds * 1000;
//Makes shure the miliseconds stay in the 3 digits range and does not count over 999
milliseconds = millisecondsV % 1000;
timerString = "Time: " + minutes + "." + (int)seconds + "." + (int)milliseconds;
//Debug.Log(minutes);
timeText.text = timerString;
//timeText.text = "Time: " + minutes + "." + Time.timeSinceLevelLoad.ToString("00.000");
//timer
if (unlimitedFule == false)
{
if (playerMScript.pp_upward)
{
float drain = 0;
drain += Time.deltaTime;
if (drain >= 0)
{
drain *= fuleDrainRate;
fule -= drain;
}
}
}
if (fule <= 0f)
{
outOfFule = true;
}
// I only inteded doritos to be used as power ups not multiple pickups so the unlimited power pick up should be spaced out so it allways are 10 sec apart.
if (unlimFuleTimer > 0)
{
unlimFuleTimer -= Time.deltaTime;
}
//Updates the unlimited fule timer.
unlimFuleText.text = unlimFuleTimer.ToString("00");
fuleLvl();
//key inputs
if (pauseScreenActive == false && allowedToPause == true)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
pauseScreen.gameObject.SetActive(true);
turnBlackscreenOn();
pauseScreenActive = true;
}
}
else
{
if (Input.GetKeyDown(KeyCode.Escape))
{
pauseScreen.gameObject.SetActive(false);
turnBlackscreenOff();
pauseScreenActive = false;
}
}
//key inputs
}
void fuleLvl()
{
if (fule > 0)
{
// 0,2 X 30 = 6
fulePerc = fule / maxFule;
if (fulePerc > 0.9)
{
p100.gameObject.SetActive(true);
p90.gameObject.SetActive(true);
p80.gameObject.SetActive(true);
p70.gameObject.SetActive(true);
p60.gameObject.SetActive(true);
p50.gameObject.SetActive(true);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.8 && fulePerc < 0.89)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(true);
p80.gameObject.SetActive(true);
p70.gameObject.SetActive(true);
p60.gameObject.SetActive(true);
p50.gameObject.SetActive(true);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.7 && fulePerc < 0.79)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(true);
p70.gameObject.SetActive(true);
p60.gameObject.SetActive(true);
p50.gameObject.SetActive(true);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.6 && fulePerc < 0.69)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(true);
p60.gameObject.SetActive(true);
p50.gameObject.SetActive(true);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.5 && fulePerc < 0.59)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(true);
p50.gameObject.SetActive(true);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.4 && fulePerc < 0.49)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(false);
p50.gameObject.SetActive(true);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.3 && fulePerc < 0.39)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(false);
p50.gameObject.SetActive(false);
p40.gameObject.SetActive(true);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.2 && fulePerc < 0.29)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(false);
p50.gameObject.SetActive(false);
p40.gameObject.SetActive(false);
p30.gameObject.SetActive(true);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.1 && fulePerc < 0.19)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(false);
p50.gameObject.SetActive(false);
p40.gameObject.SetActive(false);
p30.gameObject.SetActive(false);
p20.gameObject.SetActive(true);
p10.gameObject.SetActive(true);
}
else if (fulePerc > 0.0 && fulePerc < 0.09)
{
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(false);
p50.gameObject.SetActive(false);
p40.gameObject.SetActive(false);
p30.gameObject.SetActive(false);
p20.gameObject.SetActive(false);
p10.gameObject.SetActive(true);
}
}
else
{
//Debug.Log("out of fule!");
p100.gameObject.SetActive(false);
p90.gameObject.SetActive(false);
p80.gameObject.SetActive(false);
p70.gameObject.SetActive(false);
p60.gameObject.SetActive(false);
p50.gameObject.SetActive(false);
p40.gameObject.SetActive(false);
p30.gameObject.SetActive(false);
p20.gameObject.SetActive(false);
p10.gameObject.SetActive(false);
OutOfFuleImage.gameObject.SetActive(true);
}
}
public void pickUpInteraction(int pickupInt)
{
Debug.Log(pickupInt);
switch (pickupInt)
{
case 1:
{
rangeScan = cheeseDoodleClips.Length;
toPlay = Random.Range(0, rangeScan);
PickUpSound = cheeseDoodleClips[toPlay];
break;
}
case 2:
{
rangeScan = cheeseBallClips.Length;
toPlay = Random.Range(0, rangeScan);
PickUpSound = cheeseBallClips[toPlay];
break;
}
case 3:
{
rangeScan = DoritoClips.Length;
toPlay = Random.Range(0, rangeScan);
PickUpSound = DoritoClips[toPlay];
break;
}
case 4:
{
//dead
playerIsdead();
break;
}
case 5:
{
//winning
playerWon();
break;
}
}
interactionSoundSource.Pause();
interactionSoundSource.clip = PickUpSound;
interactionSoundSource.Play();
}
public void destroyPickUp(GameObject PickUp)
{
switch (PickUp.gameObject.tag)
{
case "cheeseDoodle":
score = score + cheeseDoodlePoints;
updateScore();
if (fule <= maxFule)
{
fule = fule + cheeseDoodlefule;
}
break;
case "cheeseBall":
score = score + cheeseBallPoints;
updateScore();
if (fule <= maxFule)
{
fule = fule + cheeseBallfule;
}
break;
case "dorito":
dorito();
updateScore();
break;
}
PickUp.SetActive(false);
ParticleSystem Ps = PickUp.gameObject.GetComponentInParent();
Ps.gameObject.GetComponent().Play();
}
void dorito()
{
fule = maxFule;
score = score + doritoPoints;
unlimitedFule = true;
unlimitedFuleImage.gameObject.SetActive(true);
unlimitedFuleImage2.gameObject.SetActive(true);
StartCoroutine(ExecuteAfterTime(unlimitedFuleTime));
// Unlimited timer
unlimFuleTimer = unlimitedFuleTime;
// Unlimited timer
IEnumerator ExecuteAfterTime(float time)
{
yield return new WaitForSeconds(time);
unlimitedFule = false;
unlimitedFuleImage.gameObject.SetActive(false);
unlimitedFuleImage2.gameObject.SetActive(false);
}
}
void playerIsdead()
{
if (allreadyDead == false)
{
allreadyDead = true;
//waits to show the death screen so you can see yourself ragdoll and explode! (Escape menu is avalible don't worry!)
StartCoroutine(waitToDie(3));
IEnumerator waitToDie(float time)
{
yield return new WaitForSeconds(time);
turnBlackscreenOn();
deadScreen.gameObject.SetActive(true);
}
}
}
void playerWon()
{
//"You got: " + score + " Cheese Points!"
winText.text = "You got: " + score + " Cheese Points!";
winTimeText.text = timerString;
StartCoroutine(waitToWin(0.6f));
IEnumerator waitToWin(float time)
{
yield return new WaitForSeconds(time);
winScreen.gameObject.SetActive(true);
turnBlackscreenOn();
}
}
void updateScore()
{
// updates the score only when points are picked up! scoreText
scoreText.text = score.ToString();
}
void turnBlackscreenOn()
{
//pauses time and showes the in game menu.
Time.timeScale = 0;
utilityButtons.gameObject.SetActive(true);
blackScreen.gameObject.SetActive(true);
Cursor.visible = true;
}
void turnBlackscreenOff()
{
// turns time back on and removes the in game menu.
Time.timeScale = 1;
utilityButtons.gameObject.SetActive(false);
blackScreen.gameObject.SetActive(false);
Cursor.visible = false;
}
void restartLvl()
{
//gets current lvl and then restart that lvl.
Scene currentScene = SceneManager.GetActiveScene(); SceneManager.LoadScene(currentScene.name);
}
void quitGame()
{
//quits application
Application.Quit();
}
void mainMenu()
{
//Goes to main menu
SceneManager.LoadScene("main_menu");
}
}
This script deals with the movement of the character, animations ragdoll, and particles. It also deals with voice lines and making sure the character only talks while the voice line plays.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_movement : MonoBehaviour
{
//scripts
public player_interaction pI;
//components=============================================================
//animators
public Animator pAnime;
Animator rocketAnime;
Animator rocketsoundAnime;
//rigidboddys
Rigidbody mainRigid;
//gameobjects
public GameObject firecracker_rat;
public GameObject gimble1;
public GameObject gimble2;
public GameObject rocketSoundObject;
public GameObject rocketBostAudio;
public GameObject voiceObject;
public GameObject interactionObject;
public GameObject deathObject;
public GameObject crashSoundObject;
public GameObject bounceSoundObject;
//particles
ParticleSystem pSystem1;
ParticleSystemRenderer pSystem1Ren;
ParticleSystem pSystem2;
ParticleSystemRenderer pSystem2Ren;
//colliders
Collider interactionCollider;
//components=============================================================
//media==================================================================
//materials
public Material blueFlame;
public Material redFlame;
//audio imports
AudioSource voiceSource;
AudioSource crashSoundSourse;
AudioSource bounceSoundScource;
AudioClip CurentVoiceClip;
public AudioClip[] cheeseDoodleVoice;
public AudioClip[] cheeseBallVoice;
public AudioClip[] doritoVoice;
public AudioClip[] laughVoice;
public AudioClip[] numnumVoice;
public AudioClip[] cheeseWinVoice;
public AudioClip[] yesyesVoice;
public AudioClip[] gonnaDieVoice;
public AudioClip[] bounceSounds;
//media==================================================================
//Ragdoll Specific game objects==========================================
public List ragdollParts = new List();
//Ragdoll Specific game objects==========================================
//floats=================================================================
public float upBoost = 10f;
public float sideBoost = 10f;
public float moveBoost = 10f;
public float playerRotation = 100f;
public float particleBoosSpeed = 5f;
public float originalParticleSpeed = 2.28f;
float playedVoice;
float SoundClipLength;
float laughWaitTime;
float soundAnimationTiming;
float timer;
public float TrueDuration;
float lastPlayedVoice;
//bools=========================================================================
bool pp_right;
bool pp_left;
bool pp_forward;
bool pp_backwards;
public bool pp_upward;
bool pp_laugh;
bool voiceIsPlayed;
bool notLoopingLaugh = false;
bool playerDead;
//int============================================================================
public int laughFrom = 30;
public int laughTo = 35;
int randomeWait;
int rangeScan;
int toPlay;
int toSayCheeseDoodle;
// The amount of time firecracker says his line on pickup of CheeseDoodles.
public int CheeseDoodleVoicerate = 15;
// Start is called before the first frame update
void Start()
{
//put ragdoll coliders in array.
//gets animator for the rigged character.
pAnime = gameObject.GetComponent();
//gets animator from child object to run two animators for the same character.
rocketAnime = firecracker_rat.GetComponent();
rocketsoundAnime = rocketBostAudio.GetComponent();
//get audio sources from child object.
voiceSource = voiceObject.GetComponent();
crashSoundSourse = crashSoundObject.GetComponent();
bounceSoundScource = bounceSoundObject.GetComponent();
//get collider from child object
interactionCollider = interactionObject.GetComponent();
//get particle emiters from su items.
pSystem1 = gimble1.GetComponent();
pSystem2 = gimble2.GetComponent();
pSystem1Ren = gimble1.GetComponent();
pSystem2Ren = gimble2.GetComponent();
mainRigid = GetComponent();
//floats
SoundClipLength = 0;
// bools to make shure the player is ideling when the game starts.
pp_left = false;
pp_right = false;
pp_forward = false;
pp_backwards = false;
pp_upward = false;
pp_laugh = false;
voiceIsPlayed = false;
playerDead = false;
//int
randomeWait = 15;
//disabled objects
deathObject.gameObject.SetActive(false);
}
// Update is called once per frame
private void Awake()
{
//removes ragdoll on awake so the player can controll the player.
SetRagdollParts();
}
//
private void SetRagdollParts()
{
// gets the rigidbodys from the children of this game object.
Rigidbody[] rigids = this.gameObject.GetComponentsInChildren();
foreach (Rigidbody r in rigids)
{
//removes the game object we want physics on.
if (r.gameObject.tag != "tail" && r.gameObject != this.gameObject)
{
// I wanted to use c.attachedRigidbody in the next for each loop instead but the player spaces out when I do.
//This makes the ragdoll not afected by gravity.
r.isKinematic = true;
}
}
//Does the same as above but for the coliders in the rag Doll
Collider[] colliders = this.gameObject.GetComponentsInChildren();
foreach (Collider c in colliders)
{
// || c.gameObject.tag != "tail" || c.gameObject.tag != "notRag"
if (c.gameObject.tag != "tail" && c.gameObject != this.gameObject)
{
//isTrigger tells the colider that it cant physacly collide with anything.
c.isTrigger = true;
//puts the ragdoll parts in a list so it can be easely used in the ragdollPlayer function.
ragdollParts.Add(c);
}
}
}
void ragdollPlayer()
{
//ragdolls player on death.
// Disables the main box colider that keeps the player from going thru the world.
//this.gameObject.GetComponentInChildren().enabled = false;
// Diables the animator for the rocket but keeps the player screaming to his death.
rocketAnime.enabled = false;
// works the same as in SetRagdollParts() but does the oposite.
Rigidbody[] rigids = this.gameObject.GetComponentsInChildren();
foreach (Rigidbody r in rigids)
{
if (r.gameObject.tag != "tail")
{
r.isKinematic = false;
//removes the velocity from the player so it falls right down. (Makes the death less glitchy)
//r.velocity = Vector3.zero;
}
}
foreach (Collider c in ragdollParts)
{
c.isTrigger = false;
}
}
void Update()
{
//only movment from keys
if (playerDead != true)
{
if (Input.GetKey(KeyCode.W))
{
mainRigid.AddForce(transform.forward * Time.deltaTime * moveBoost);
}
if (Input.GetKey(KeyCode.S))
{
mainRigid.AddForce(-transform.forward * Time.deltaTime * moveBoost);
}
if (Input.GetKey(KeyCode.Space))
{
mainRigid.AddForce(Vector3.up * Time.deltaTime * upBoost, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.D))
{
mainRigid.AddForce(transform.right * Time.deltaTime * sideBoost, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.A))
{
mainRigid.AddForce(-transform.right * Time.deltaTime * sideBoost, ForceMode.Impulse);
}
//mouse movement
if (Input.GetAxis("Mouse X") < 0)
{
transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * playerRotation, Space.World);
}
if (Input.GetAxis("Mouse X") > 0)
{
transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * playerRotation, Space.World);
}
}
//gets keyinputs and plays animations on key presses.
if (Input.GetKeyDown(KeyCode.A))
{
//Debug.Log("Player holding A!");
pp_left = true;
}
if (Input.GetKeyUp(KeyCode.A))
{
//Debug.Log("Player not holding A!");
pp_left = false;
}
if (Input.GetKeyDown(KeyCode.D))
{
//Debug.Log("Player holding D!");
pp_right = true;
}
if (Input.GetKeyUp(KeyCode.D))
{
//Debug.Log("Player not holding D!");
pp_right = false;
}
if (Input.GetKeyDown(KeyCode.W))
{
pp_forward = true;
}
if (Input.GetKeyUp(KeyCode.W))
{
pp_forward = false;
}
if (Input.GetKeyDown(KeyCode.S))
{
pp_backwards = true;
}
if (Input.GetKeyUp(KeyCode.S))
{
pp_backwards = false;
}
//emitrate on space
if (Input.GetKeyDown(KeyCode.Space))
{
BoostParticles();
pp_upward = true;
}
if (Input.GetKeyUp(KeyCode.Space))
{
ResetParticles();
pp_upward = false;
}
// plays animator
//pAnime
if (pp_left == false)
{
pAnime.SetBool("holdLeft", false);
rocketAnime.SetBool("tiltLeft", false);
}
if (pp_left == true)
{
pAnime.SetBool("holdLeft", true);
rocketAnime.SetBool("tiltLeft", true);
}
if (pp_right == false)
{
pAnime.SetBool("holdRight", false);
rocketAnime.SetBool("tiltRight", false);
}
if (pp_right == true)
{
pAnime.SetBool("holdRight", true);
rocketAnime.SetBool("tiltRight", true);
}
//character interaction anime
if (pp_laugh == false)
{
pAnime.SetBool("laughing", false);
}
if (pp_laugh == true)
{
pAnime.SetBool("laughing", true);
}
//rocketAnime
if (pp_forward == false)
{
rocketAnime.SetBool("tiltForward", false);
}
if (pp_forward == true)
{
rocketAnime.SetBool("tiltForward", true);
}
if (pp_backwards == false)
{
rocketAnime.SetBool("tiltBackward", false);
}
if (pp_backwards == true)
{
rocketAnime.SetBool("tiltBackward", true);
}
// rocketSoundAnime
if (pp_upward == false)
{
//Debug.Log("HoldingUp");
rocketsoundAnime.SetBool("holdingUp", false);
}
if (pp_upward == true)
{
//Debug.Log("not HoldingUp!");
rocketsoundAnime.SetBool("holdingUp", true);
}
//voice code
//Randome laugh
//Debug.Log(playerDead);
if (voiceIsPlayed == false)
{
if (playerDead == false)
{
//runs the laugh after a time
StartCoroutine(ExecuteAfterTime(randomeWait));
//function to run the laugh
IEnumerator ExecuteAfterTime(float time)
{
//checks if it's allready waiting to play the laugh.
if (notLoopingLaugh)
//breaks if the laugh is being waited for.
yield break;
notLoopingLaugh = true;
//Waiting fuction
yield return new WaitForSeconds(time);
//executes the laugh
playedVoice = 1;
voicePlayer(playedVoice);
// picks a randome time to play the next laugh
randomeWait = Random.Range(laughFrom, laughTo);
//This bool tels the script that it's alowed to run this script again after the time.
notLoopingLaugh = false;
}
}
}
if (pI.outOfFule)
{
wallHit();
}
//voiceTimer
//Debug.Log(SoundClipLength);
//makes shure there is an active soundclip
if (SoundClipLength > 0)
{
//converts the value to another float so we can reset the float to not constantly refill it.
TrueDuration = SoundClipLength;
}
// Sets a bool that the voice is being played
voiceIsPlayed = true;
//starts the timer by removing the time acording to the time left in the float.
TrueDuration -= Time.deltaTime;
// when there is no time left it lets the next voce be played.
if (TrueDuration < 0)
{
voiceIsPlayed = false;
TrueDuration = 0;
}
//resets the soundclip duration so we only refll the timer when there is an active clip playing.
SoundClipLength = 0;
//voiceTimer
}
void BoostParticles()
{
// you have to use main here for some reason.
var main = pSystem1.main;
//Boosts the particle speed making it emit faster
main.startSpeed = particleBoosSpeed;
var main2 = pSystem2.main;
main2.startSpeed = particleBoosSpeed;
// Changes the material of the flames to blue
pSystem1Ren.material = blueFlame;
pSystem2Ren.material = blueFlame;
}
void ResetParticles()
{
var main = pSystem1.main;
main.startSpeed = originalParticleSpeed;
var main2 = pSystem2.main;
main2.startSpeed = originalParticleSpeed;
pSystem1Ren.material = redFlame;
pSystem2Ren.material = redFlame;
}
void voicePlayer(float playedVoice)
{
// Makes shure you die correctly.
if (playedVoice == 5)
{
voiceIsPlayed = false;
TrueDuration = 0;
}
// To interupt the laugh to make shure important dialog goes first
if (lastPlayedVoice == 1)
{
voiceIsPlayed = false;
TrueDuration = 0;
}
lastPlayedVoice = playedVoice;
// Makes shure the character is not saying anyting or is dead.
if (voiceIsPlayed == false && playerDead == false)
{
switch (playedVoice)
{
case 1:
// gets how long the array is
rangeScan = laughVoice.Length;
//selects a random nr from the leth of the array
toPlay = Random.Range(0, rangeScan);
//choses a random nr fro mthe array;
CurentVoiceClip = laughVoice[toPlay];
break;
case 2:
rangeScan = cheeseDoodleVoice.Length;
toPlay = Random.Range(0, rangeScan);
CurentVoiceClip = cheeseDoodleVoice[toPlay];
break;
case 3:
rangeScan = cheeseBallVoice.Length;
toPlay = Random.Range(0, rangeScan);
CurentVoiceClip = cheeseBallVoice[toPlay];
break;
case 4:
rangeScan = doritoVoice.Length;
toPlay = Random.Range(0, rangeScan);
CurentVoiceClip = doritoVoice[toPlay];
break;
case 5:
rangeScan = gonnaDieVoice.Length;
toPlay = Random.Range(0, rangeScan);
CurentVoiceClip = gonnaDieVoice[toPlay];
break;
case 6:
rangeScan = cheeseWinVoice.Length;
toPlay = Random.Range(0, rangeScan);
CurentVoiceClip = cheeseWinVoice[toPlay];
break;
}
Debug.Log(CurentVoiceClip);
//pauses the last played clip.
voiceSource.Pause();
//swiches clip.
voiceSource.clip = CurentVoiceClip;
//plays the new one.
voiceSource.Play();
// SoundClipLegth will be used to tell the animation engine how long to animate the talking/laghing loop.
SoundClipLength = CurentVoiceClip.length;
// Ends the animation a little sooner becuse of dead air in audio clips from encoding.
soundAnimationTiming = SoundClipLength - 1.5f;
//laughWaitTime tells the laghloop how long to wait to start again.
laughWaitTime = SoundClipLength;
// Sets the bool to stop the laugh loop and stopes the character from speaking when saying lines.
voiceIsPlayed = true;
//This controlls the animation on speach (at the moment laugh is used for all animations but will change in the future)
pp_laugh = true;
StartCoroutine(LaughAnimationTime(soundAnimationTiming));
IEnumerator LaughAnimationTime(float time)
{
yield return new WaitForSeconds(time);
pp_laugh = false;
}
}
}
//colider functions
void OnTriggerEnter(Collider other)
{
switch (other.tag)
{
case "cheeseDoodle":
{
pI.pickUpInteraction(1);
toSayCheeseDoodle = Random.Range(1, 100);
if (toSayCheeseDoodle < CheeseDoodleVoicerate)
{
playedVoice = 2;
voicePlayer(playedVoice);
}
pI.destroyPickUp(other.gameObject);
//other.gameObject.SetActive(false);
}
break;
case "cheeseBall":
{
pI.pickUpInteraction(2);
playedVoice = 3;
voicePlayer(playedVoice);
pI.destroyPickUp(other.gameObject);
}
break;
case "dorito":
{
pI.pickUpInteraction(3);
playedVoice = 4;
voicePlayer(playedVoice);
pI.destroyPickUp(other.gameObject);
}
break;
case "safe":
{
safeLanding();
}
break;
case "wall":
{
wallHit();
}
break;
case "ground":
{
playerDead = true;
//deathObject creates an expolsion with sound and particles on activation.
deathObject.gameObject.SetActive(true);
removeRocketFire();
ragdollPlayer();
pAnime.enabled = false;
//voiceObject.gameObject.SetActive(false);
pI.pickUpInteraction(4);
}
break;
case "winningGround":
{
playedVoice = 6;
voicePlayer(playedVoice);
safeLanding();
removeRocketFire();
// Does not kill the player but turns of movement.
playerDead = true;
rocketAnime.enabled = false;
pI.pickUpInteraction(5);
}
break;
}
}
void wallHit()
{
//plays crash sound
crashSoundSourse.Play();
playedVoice = 5;
voicePlayer(playedVoice);
playerDead = true;
removeRocketFire();
ragdollPlayer();
// showes in game menu if it takes to long for the player to hit the ground!
StartCoroutine(waitToDie(7));
IEnumerator waitToDie(float time)
{
yield return new WaitForSeconds(time);
pI.pickUpInteraction(4);
}
}
void removeRocketFire()
{
//stops rocket sound
rocketBostAudio.gameObject.SetActive(false);
rocketSoundObject.gameObject.SetActive(false);
//Stops particle system
pSystem1.Stop();
pSystem2.Stop();
}
void safeLanding()
{
//plays a random bounce sound when player hits somthing safe.
rangeScan = bounceSounds.Length;
toPlay = Random.Range(0, rangeScan);
CurentVoiceClip = bounceSounds[toPlay];
bounceSoundScource.clip = CurentVoiceClip;
bounceSoundScource.Play();
}
}
This website uses cookies to keep track of the traffic to this website. If you want to keep using this website, then you must accept our policy: More information/policy