Browse Source

-Merged Player controllers into one tileable script.

-Added attacks.
-Spotlights now point at player instead of follow them
weapons
Joshua Reason 9 years ago
parent
commit
3476db4564
12 changed files with 271 additions and 16 deletions
  1. BIN
      playable/Assets/Confetti.prefab
  2. BIN
      playable/Assets/Lachlan_jump_work.unity
  3. +11
    -8
      playable/Assets/Scripts/PlayerControler.cs
  4. +1
    -1
      playable/Assets/Scripts/confettiController.cs
  5. +11
    -7
      playable/Assets/Scripts/player2_controls.cs
  6. +162
    -0
      playable/Assets/Scripts/playerController.cs
  7. +8
    -0
      playable/Assets/Scripts/playerController.cs.meta
  8. +10
    -0
      playable/Assets/Scripts/spotLightController.cs
  9. +8
    -0
      playable/Assets/Scripts/spotLightController.cs.meta
  10. +52
    -0
      playable/Assets/stickController.cs
  11. +8
    -0
      playable/Assets/stickController.cs.meta
  12. BIN
      playable/ProjectSettings/InputManager.asset

BIN
playable/Assets/Confetti.prefab View File


BIN
playable/Assets/Lachlan_jump_work.unity View File


+ 11
- 8
playable/Assets/Scripts/PlayerControler.cs View File

@ -14,6 +14,7 @@ public class PlayerControler : MonoBehaviour {
public int health = 0;
private int displayedHealth = 0;
private bool updateHealth = false;
private float curDirection = -1;
public GameObject bullet;
public GameObject heart;
bool canJump = true;
@ -27,7 +28,8 @@ public class PlayerControler : MonoBehaviour {
rigidbody2D.velocity = velo;
animator.SetFloat("Velocity",velo.magnitude);
animator.SetBool ("Fall", !canJump);
applyPlayerDirection (Input.GetAxisRaw ("Horizontal_P1"));
if (curDirection != Input.GetAxisRaw ("Horizontal_P1"))
applyPlayerDirection (Input.GetAxisRaw ("Horizontal_P1"));
if (displayedHealth != health) {
updateHealth = true;
@ -71,9 +73,8 @@ public class PlayerControler : MonoBehaviour {
{
if (moveHorizontal != 0)
{
Vector3 scale = transform.localScale;
scale.x = -moveHorizontal * Mathf.Abs(scale.x);
transform.localScale = scale;
transform.Rotate(0,180,0);
curDirection = moveHorizontal;
}
}
@ -87,9 +88,11 @@ public class PlayerControler : MonoBehaviour {
}
private void death(){
Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0);
health = 3;
transform.position = spawnPos;
if (health == 0) {
Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0);
health = 3;
transform.position = spawnPos;
}
}
void OnCollisionEnter2D(Collision2D col){
@ -99,7 +102,7 @@ public class PlayerControler : MonoBehaviour {
canJump = true;
if(transform.position.y>col.transform.position.y){
col.gameObject.GetComponent<player2_controls>().health --;
Confettishoot(col.gameObject.transform.position,200.0f);
Confettishoot(col.gameObject.transform.position,20.0f);
//health --;
rigidbody2D.velocity = jumpVector;
canJump = false;

+ 1
- 1
playable/Assets/Scripts/confettiController.cs View File

@ -15,7 +15,7 @@ public class confettiController : MonoBehaviour {
int randColour = Mathf.FloorToInt (rand);
startTime = Time.time;
startScale = transform.localScale.x;
rigidbody.velocity = Random.onUnitSphere * speed;
rigidbody2D.velocity = Random.onUnitSphere * speed;
switch (randColour) {
case 0:

+ 11
- 7
playable/Assets/Scripts/player2_controls.cs View File

@ -7,28 +7,33 @@ public class player2_controls : MonoBehaviour {
void Start () {
}
}
public float speed;
public Vector2 jumpVector;
public int health = 0;
private int displayedHealth = 0;
private bool updateHealth = false;
private float curDirection = 1;
public GameObject bullet;
public GameObject heart;
bool canJump = true;
public string HorizontalBtn;
public GameObject newConfetti;
//Horizontal_P2
void Update() {
Vector2 velo = rigidbody2D.velocity;
velo.x = Input.GetAxis ("Horizontal_P2") * speed;
velo.x = Input.GetAxis (HorizontalBtn) * speed;
rigidbody2D.velocity = velo;
animator.SetFloat("Velocity",velo.magnitude);
animator.SetBool ("Fall", !canJump);
applyPlayerDirection (Input.GetAxisRaw ("Horizontal_P2"));
if (curDirection != Input.GetAxisRaw (HorizontalBtn))
applyPlayerDirection (Input.GetAxisRaw (HorizontalBtn));
if (displayedHealth != health) {
updateHealth = true;
@ -71,9 +76,8 @@ public class player2_controls : MonoBehaviour {
{
if (moveHorizontal != 0)
{
Vector3 scale = transform.localScale;
scale.x = moveHorizontal * Mathf.Abs(scale.x);
transform.localScale = scale;
transform.Rotate(0,180,0);
curDirection = moveHorizontal;
}
}
@ -99,7 +103,7 @@ public class player2_controls : MonoBehaviour {
else if (col.collider.tag == "Player") {
if(transform.position.y>col.transform.position.y){
col.gameObject.GetComponent<PlayerControler>().health --;
Confettishoot(col.gameObject.transform.position,200.0f);
Confettishoot(col.gameObject.transform.position,20.0f);
//health --;
rigidbody2D.velocity = jumpVector;

+ 162
- 0
playable/Assets/Scripts/playerController.cs View File

@ -0,0 +1,162 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //input for lists
public class playerController : MonoBehaviour {
public string HorizontalBtn; //holds string for horizontal input
public string VerticalBtn; //holds string for Vertical input
public string StrongAttackBtn;
public Animator animator; //Holds animator for Pinata
public GameObject heart; //Health sprites
public GameObject newConfetti; //confetti when hit
public stickController stick; //controller for stick
public float runSpeed; //run speed
public float jumpHeight; //jump height
public float health; //holds health
public int confettiOnHit; //amount of confetti created on hit
public Vector2 healthPos;
private float displayedHealth = 3; //currently displayed health
private bool updateHealth = true; //if the health bar needs updating
private float curDirection = -1; //direction player is pointing
private bool canJump = true; // if player can jump
private List<GameObject> hearts = new List<GameObject> (); //list which holds health sprites;
private bool isSpinning = false; //checks if player is spin attacking
void Start(){
if (transform.rotation.y == 180)
curDirection = -1;
else
curDirection = 1;
}
// Update is called once per frame
void Update () {
displayHealth ();
death ();
applyPlayerDirection (Input.GetAxisRaw (HorizontalBtn));
}
void FixedUpdate() {
/*
* Movement inputs
*/
Vector2 velocity = rigidbody2D.velocity;
velocity.x = Input.GetAxis (HorizontalBtn) * runSpeed; //Horizontal input
if ((Input.GetAxisRaw (VerticalBtn) == 1) && (canJump)) //Vertical input
{
velocity.y = jumpHeight;
canJump = false;
}//end if
if (Input.GetAxisRaw (StrongAttackBtn)==1) {
if (!isSpinning)
StartCoroutine (spinAttack (Vector3.down * 360, 0.2f));
}
rigidbody2D.velocity = velocity; //apply inputs
animator.SetFloat("Velocity",velocity.magnitude);//inputs for animator
animator.SetBool ("Fall", !canJump);
}//end fixed update
private void displayHealth(){
if (displayedHealth != health) //check if health needs updating
updateHealth = true;
if (updateHealth){
foreach (GameObject desHeart in hearts) //Destroy all heart sprites
GameObject.Destroy (desHeart);
hearts.Clear(); //sets list to zero
for (int i=1; i<= health; i++) { //creates new heart sprite for each health
GameObject heartCanister = Instantiate (heart) as GameObject; //creat heart sprite
Vector3 heartPos = new Vector3 ();
//set position
if (healthPos.x == 1)
heartPos.x = (i * 0.033f);
else
heartPos.x = 1-(i * 0.033f);
if (healthPos.y == 1)
heartPos.y = 0.95f;
else
heartPos.y = 0.05f;
heartCanister.transform.position = heartPos;
hearts.Add(heartCanister); //adds heart to list
}//end for
displayedHealth = health;
updateHealth = false;
}//end if
}//end displayHealth
//respawns player if they die
private void death(){
if (health <= 0) {
Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0); //picks random position
health = 3; //resets life
transform.position = spawnPos; //changes position
}//end if
}//end death
private void applyPlayerDirection(float moveHorizontal)
{
if ((curDirection != moveHorizontal) && (moveHorizontal != 0) && !isSpinning) //if player movement direction vs displayed direction
{
transform.Rotate(0,180,0); //rotates player
curDirection = moveHorizontal; //updates direction
}
}
IEnumerator spinAttack(Vector3 byAngles, float inTime) {
isSpinning = true;
stick.isAttacking = true;
Debug.Log ("" + gameObject.name + "spinning");
Quaternion startAngle = transform.rotation;
Quaternion endAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
float direction = transform.eulerAngles.y;
for(float i = 0; i < 1; i += Time.deltaTime/inTime) {
transform.rotation = Quaternion.Euler(0,direction,Mathf.Lerp(0,360,i));
yield return null;
}
transform.eulerAngles = new Vector3 (0,direction,0);
Debug.Log ("" + gameObject.name + "End spin");
isSpinning = false;
stick.isAttacking = false;
}
void OnCollisionEnter2D(Collision2D col){
if (col.collider.tag == "ground")
canJump = true;
else if (col.collider.tag == "Player") {
canJump = true;
}
}
}

+ 8
- 0
playable/Assets/Scripts/playerController.cs.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: feb419ae5508b15478825d388564e732
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

+ 10
- 0
playable/Assets/Scripts/spotLightController.cs View File

@ -0,0 +1,10 @@
using UnityEngine;
using System.Collections;
public class spotLightController : MonoBehaviour {
public Transform target;
void Update() {
transform.LookAt(target);
}
}

+ 8
- 0
playable/Assets/Scripts/spotLightController.cs.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 72c6d3c5f1bbc484bbf1d4932cb997d9
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

+ 52
- 0
playable/Assets/stickController.cs View File

@ -0,0 +1,52 @@
using UnityEngine;
using System.Collections;
public class stickController : MonoBehaviour {
public GameObject newConfetti;
public bool isAttacking;
public bool isSpinning;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (isAttacking) {
gameObject.GetComponent<TrailRenderer> ().enabled = true;
gameObject.GetComponent<BoxCollider2D> ().enabled = true;
} else {
gameObject.GetComponent<TrailRenderer> ().enabled = false;
gameObject.GetComponent<BoxCollider2D> ().enabled = false;
}
}
private void Confettishoot(Vector3 target,int size){ //creates confetti
Debug.Log ("Creating Confetti");
target.y += 1.5f;
for (int i = 0; i <size; i++) {
Instantiate (newConfetti, target, transform.rotation);
}
}
void OnCollisionEnter2D(Collision2D col){
if (col.collider.tag == "Player") {
if(transform.position.y>col.transform.position.y){
if (isAttacking){
col.gameObject.GetComponent<playerController>().health --;
Confettishoot(col.gameObject.transform.position,col.gameObject.GetComponent<playerController>().confettiOnHit);
Vector2 velocity = col.rigidbody.velocity;
velocity.y = 22;
col.rigidbody.velocity = velocity;
}
}
}
}
}

+ 8
- 0
playable/Assets/stickController.cs.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 34ac62105d880784f9e51ad9f26a0111
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

BIN
playable/ProjectSettings/InputManager.asset View File


Loading…
Cancel
Save