Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:35
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 JonathanYin/8ff41ecb4a4c262246d1068d857afe8b to your computer and use it in GitHub Desktop.
Save JonathanYin/8ff41ecb4a4c262246d1068d857afe8b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Pancam : MonoBehaviour
{
float ydir = 0f;
public GameObject player;
public GameObject player2;
//for our GUIText object and our score
public GUIElement gui;
public GUIElement gui2;
float playerScore = 0;
float player2Score = 0;
public GUIText myGUIText;
public GUIText myGUIText2;
private string scoreVariable;
private string scoreVariable2;
//this function updates our guitext object
void Start()
{
player.tag = "Player1";
GUIComponent.text = "P1";
GUIComponent1.text = "P2";
player2.tag = "Player2";
}
void OnGUI()
{
myGUIText.text = scoreVariable;
//GUIText.text = "Score: " + ((int)(playerScore * 10)).ToString();
scoreVariable = "P1 Score: " + ((int)(playerScore * 10)).ToString();
myGUIText2.text = scoreVariable2;
//GUIText.text = "Score: " + ((int)(playerScore * 10)).ToString();
scoreVariable2 = "P2 Score: " + ((int)(player2Score * 10)).ToString();
}
//this is generic function we can call to increase the score by an amount
public void IncreaseScore(int amount)
{
playerScore += amount;
}
public void IncreaseScore2(int amount)
{
player2Score += amount;
}
void OnDisable()
{
PlayerPrefs.SetInt("Score", (int)(playerScore));
PlayerPrefs.SetInt("Score2", (int)(player2Score));
}
// Update is called once per frame
void Update()
{
if (player || player2)
{
if (player && player.transform.position.x > -1)
{
//this moves the camera
float randy = 0f;
randy = Random.Range(0f, 100f);
if (randy < 20)
{
ydir = ydir + .005f;
}
else if (randy > 20 && randy < 40)
{
ydir = ydir - .005f;
}
else if (randy > 80)
{
ydir = 0f;
}
transform.position = new Vector3(transform.position.x + 0.03f, transform.position.y + ydir, -10);
}
else if (player2 && player2.transform.position.x > -1)
{
//this moves the camera
float randy = 0f;
randy = Random.Range(0f, 100f);
if (randy < 20)
{
ydir = ydir + .005f;
}
else if (randy > 20 && randy < 40)
{
ydir = ydir - .005f;
}
else if (randy > 80)
{
ydir = 0f;
}
transform.position = new Vector3(transform.position.x + 0.03f, transform.position.y + ydir, -10);
}
}
if (player && player.transform.position.x > -1)
{
//update our score every tick of the clock
playerScore += Time.deltaTime;
}
if (player2 && player2.transform.position.x > -1)
{
//update our score every tick of the clock
player2Score += Time.deltaTime;
}
if (player == null && player2 == null)
{
SceneManager.LoadScene(2);
/*RobotController.maxSpeed = 9;
GUIComponent.text = "P1";
player.tag = "Player1";
RobotController1.maxSpeed = 9;
GUIComponent1.text = "P2";
player2.tag = "Player2";
*/
}
//if either of the players are moving at super speed, then the camera will move faster too
if (player)
{
if (player.tag == "Immune")
{
//this moves the camera
float randy = 0f;
randy = Random.Range(0f, 100f);
if (randy < 20)
{
ydir = ydir + .005f;
}
else if (randy > 20 && randy < 40)
{
ydir = ydir - .005f;
}
else if (randy > 80)
{
ydir = 0f;
}
transform.position = new Vector3(transform.position.x + 0.02f, transform.position.y + ydir, -10);
playerScore += Time.deltaTime;
playerScore += Time.deltaTime;
}
}
if (player2)
{
if (player2.tag == "Immune")
{
//this moves the camera
float randy = 0f;
randy = Random.Range(0f, 100f);
if (randy < 20)
{
ydir = ydir + .005f;
}
else if (randy > 20 && randy < 40)
{
ydir = ydir - .005f;
}
else if (randy > 80)
{
ydir = 0f;
}
transform.position = new Vector3(transform.position.x + 0.02f, transform.position.y + ydir, -10);
player2Score += Time.deltaTime;
player2Score += Time.deltaTime;
}
}
}
}
@JonathanYin
Copy link
Author

This script increases the players' scores if they are alive, and moves the screen along so long as one of them is beyond the x = -1 position. If one player dies, and the other is alive, the game continues, but the dead player does not gain points anymore. If one of the players is in an "Immune" status, the game moves faster and that player gains points at twice the rate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment