Pieces of the code used to make this
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Playermovement : MonoBehaviour {
public Rigidbody2D rb; //used to change velocity
public Text countBounces;
public Vector3 velocity = new Vector3(3, -3, 0); //velocity has been changed in the unity editor
public float windSpeed = 5f; //accelerates the player his velocity X
public float maxWindSpeed = 30;
public Vector3 gravity = new Vector3(0,-15f,0); //normal gravity
public float diveGravity = 30f; //if the player dives use this gravity instead
public float bounciness = -0.7f; //by a bounce revert the velocity Y
public AudioClip SND_plusOne, SND_plusTwo, SND_plusFive;
public int Bounces, highscore;
public bool didDive = false; //used to dive and increase the gravity
public bool hasResetVelocity = false; //used to have the jump ability
public bool isOofBounds = false; //if the player is out of the map
public bool hasWon; //if the player has won
public GameObject tempVar;
public Sprite spr_Circle, spr_Square; //used to change the sprite of player for more feedback
public SpriteRenderer spriteRenderer; //used to change the sprite of player for more feedback
// Use this for initialization
void Start () {
highscore = PlayerPrefs.GetInt("Lowest Bounces", 10000); //get the lowest bounce from memory, if(none) then set it to 10000
rb = GetComponent(); //init the rigidbody
Bounces = 0; //set score towards 0
spriteRenderer = GetComponent();
countBounces.text = "Bounces Count: " + Bounces.ToString() + "\nBest lowest score: " + highscore; //displays score and highscore
}
// Update is called once per frame
void Update () {
normalInput();
//note: i wanted to do some more stuff in here
}
void OnCollisionEnter2D(Collision2D coll) {
//if player collides with a platform
if (coll.gameObject.tag == "Platform") {
velocity.y *= bounciness; //change the y velocity for a bounce
didDive = false; //reset the didDive for another increased gravity
windSpeed = 5; //
Bounces++; //increases the score by one
countBounces.text = "Bounces Count: " + Bounces.ToString() + "\nBest lowest score: " + highscore; //refresh the score on screen
AudioSource.PlayClipAtPoint(SND_plusOne, transform.position, 0.4f); //play sound
spriteRenderer.sprite = spr_Circle; //change the sprite back to a circle
}
}
//if the player collects a power up
void OnTriggerEnter2D(Collider2D collider) {
if (collider.gameObject.tag == "PowerUp") {
hasResetVelocity = true; //can use the jump ability
AudioSource.PlayClipAtPoint(SND_plusFive, transform.position); //play feedback
DestroyObject(collider.gameObject); //destroy powerupp
}
}
void chronoBreak() {
//stuff to add later into the game
}
void normalInput() {
//if the player uses a dive
if (Input.GetKeyDown(KeyCode.Space) && !didDive) {
didDive = true;
velocity.y *= 0.4f;
//< velocity.x *= 0.3f; >// //*the other way of slowing the velocity X*//
windSpeed = -20f; //slows the player in velocity X
spriteRenderer.sprite = spr_Square; //increase feedback by changing the sprite
}
// use a jump
if (Input.GetKey(KeyCode.RightShift) | Input.GetKey(KeyCode.LeftShift) && hasResetVelocity) {
hasResetVelocity = false;
velocity = new Vector3(20, 12);
didDive = false;
spriteRenderer.sprite = spr_Circle; //reset the sprite back to the circle
AudioSource.PlayClipAtPoint(SND_plusTwo, transform.position);
//< Bounces += 5; >// // for testing only //not implemented because it punishes the player for using it
countBounces.text = "Bounces Count: " + Bounces.ToString() + "\nBest lowest score: " + highscore; //refresh the score on screen
}
if (velocity.x < 5) { windSpeed = 5; } //keeps the velocity X on a minimum of 5 /*this has to be changed to remove the bug when you collide to the left side of a platform*/
velocity.x += windSpeed * Time.deltaTime; //accelerates velocity X with the windspeed over time
if (didDive) velocity.y -= diveGravity * Time.deltaTime; //if didDive then increase gravity
else velocity += gravity * Time.deltaTime; //if didn't dive then just use normal gravity
if (velocity.x > maxWindSpeed) velocity.x = maxWindSpeed; //if velocity x is higher then the maxspeed set it to maxspeed
rb.velocity = velocity; //set the velocity
//if player is below the map start game over script
if (transform.position.y < -10) {
if (!isOofBounds) {
isOofBounds = true;
transform.gameObject.AddComponent();
}
}
}
}