mirror of
https://github.com/ConjureETS/Labo_2_Equ_3_a15.git
synced 2026-03-24 02:00:57 +00:00
Health Pickup
This commit is contained in:
parent
91062aa716
commit
79da09f3da
9
Assets/Script/Pickup Scripts/HealthPickup.cs
Normal file
9
Assets/Script/Pickup Scripts/HealthPickup.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections;
|
||||
|
||||
public class HealthPickup : MonoBehaviour
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class BallShoot : MonoBehaviour {
|
||||
|
||||
public class BallShoot: MonoBehaviour {
|
||||
|
||||
public float speed;
|
||||
public float distanceShoot=15;
|
||||
public GameObject theParent;
|
||||
@ -13,12 +13,12 @@ public class BallShoot : MonoBehaviour {
|
||||
transform.rotation = new Quaternion (theParent.transform.rotation.x, theParent.transform.rotation.y, -90.0f, theParent.transform.rotation.w);
|
||||
} else
|
||||
transform.rotation = new Quaternion (theParent.transform.rotation.x, theParent.transform.rotation.y,90.0f, theParent.transform.rotation.w);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
|
||||
}
|
||||
void FixedUpdate ()
|
||||
{
|
||||
151
Assets/Script/Player Scripts/PlayerControl.cs
Normal file
151
Assets/Script/Player Scripts/PlayerControl.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class PlayerControl : MonoBehaviour
|
||||
{
|
||||
[HideInInspector]
|
||||
public bool facingRight = true;
|
||||
[HideInInspector]
|
||||
public bool jump = false;
|
||||
[HideInInspector]
|
||||
public bool fire = false;
|
||||
[HideInInspector]
|
||||
public bool power = false;
|
||||
[HideInInspector]
|
||||
public bool watch = false;
|
||||
|
||||
|
||||
public float moveForce = 365f;
|
||||
public float maxSpeed = 5f;
|
||||
public float vie = 100;
|
||||
public AudioClip[] jumpClips;
|
||||
public float jumpForce = 1000f;
|
||||
public BallShoot ballPrefab;
|
||||
private Transform groundCheck;
|
||||
private Transform gunCheck;
|
||||
private bool grounded = false;
|
||||
private Animator anim;
|
||||
|
||||
private float canShoot = 0;
|
||||
private float invulnerable = 0;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
groundCheck = transform.Find("groundCheck");
|
||||
gunCheck = transform.Find("gunCheck");
|
||||
anim = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
|
||||
if (Input.GetButtonDown("Jump") && grounded)
|
||||
jump = true;
|
||||
|
||||
if (Input.GetButtonDown("Fire1"))
|
||||
fire = true;
|
||||
else if (Input.GetButtonUp("Fire1"))
|
||||
fire = false;
|
||||
if (Input.GetButtonDown("Fire2"))
|
||||
power = true;
|
||||
else if (Input.GetButtonUp("Fire2"))
|
||||
power = false;
|
||||
if (Input.GetButtonDown("Fire3"))
|
||||
watch = true;
|
||||
else if (Input.GetButtonUp("Fire3"))
|
||||
watch = false;
|
||||
}
|
||||
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
float h = Input.GetAxis("Horizontal");
|
||||
if (h != 0)
|
||||
anim.SetBool("Run", true);
|
||||
else
|
||||
anim.SetBool("Run", false);
|
||||
if (h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
|
||||
GetComponent<Rigidbody2D>().AddForce(Vector2.right * h * moveForce);
|
||||
if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
|
||||
GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
|
||||
if (h > 0 && !facingRight)
|
||||
Flip();
|
||||
else if (h < 0 && facingRight)
|
||||
Flip();
|
||||
if (jump)
|
||||
{
|
||||
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
|
||||
jump = false;
|
||||
}
|
||||
if (!grounded)
|
||||
{
|
||||
anim.SetBool("Jump", true);
|
||||
}
|
||||
else
|
||||
anim.SetBool("Jump", false);
|
||||
|
||||
if (watch)
|
||||
{
|
||||
anim.SetBool("Watch", true);
|
||||
Time.timeScale = 0.0f;
|
||||
vie = 100;
|
||||
}
|
||||
else
|
||||
anim.SetBool("Watch", false);
|
||||
if (power)
|
||||
{
|
||||
anim.SetBool("Power", true);
|
||||
}
|
||||
else
|
||||
anim.SetBool("Power", false);
|
||||
|
||||
if (fire)
|
||||
{
|
||||
anim.SetBool("Shoot", true);
|
||||
if (canShoot <= 0)
|
||||
{
|
||||
Instantiate(ballPrefab, gunCheck.position, Quaternion.identity);
|
||||
GameObject[] ball = GameObject.FindGameObjectsWithTag("ball");
|
||||
BallShoot ballR = ball[ball.Length - 1].GetComponent<BallShoot>();
|
||||
ballR.theParent = this.gameObject;
|
||||
canShoot = 0.3f;
|
||||
}
|
||||
}
|
||||
else
|
||||
anim.SetBool("Shoot", false);
|
||||
|
||||
canShoot -= Time.deltaTime;
|
||||
invulnerable -= Time.deltaTime;
|
||||
}
|
||||
public void getHurt(float lessLife)
|
||||
{
|
||||
if (invulnerable <= 0)
|
||||
{
|
||||
vie -= lessLife;
|
||||
invulnerable = 0.5f;
|
||||
if (vie <= 0)
|
||||
Time.timeScale = 0.0f;
|
||||
}
|
||||
}
|
||||
void OnTriggerEnter2D(Collider2D col)
|
||||
{
|
||||
if (col.tag == "mechant")
|
||||
{
|
||||
print("mechant");
|
||||
if (invulnerable <= 0)
|
||||
{
|
||||
jump = true;
|
||||
getHurt(25);
|
||||
}
|
||||
}
|
||||
}
|
||||
void Flip()
|
||||
{
|
||||
facingRight = !facingRight;
|
||||
|
||||
Vector3 theScale = transform.localScale;
|
||||
theScale.x *= -1;
|
||||
transform.localScale = theScale;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class PlayerControl : MonoBehaviour
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[HideInInspector]
|
||||
public bool facingRight = true;
|
||||
56
Assets/Script/Player Scripts/PlayerHealth.cs
Normal file
56
Assets/Script/Player Scripts/PlayerHealth.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections;
|
||||
|
||||
public class PlayerHealth : MonoBehaviour
|
||||
{
|
||||
|
||||
public int startingHealth = 100; // The amount of health the player starts the game with.
|
||||
public int currentHealth; // The current health the player has.
|
||||
public int healthToRecoverOnPickup;
|
||||
public Slider healthSlider; // Reference to the UI's health bar.
|
||||
public AudioClip deathClip; // The audio clip to play when the player dies.
|
||||
public float flashSpeed = 5f; // The speed the damageImage will fade at.
|
||||
public Color flashColour = new Color(1f, 0f, 0f, 0.1f); // The colour the damageImage is set to, to flash.
|
||||
|
||||
|
||||
Animator anim; // Reference to the Animator component.
|
||||
AudioSource playerAudio; // Reference to the AudioSource component.
|
||||
PlayerController playerController; // Reference to the player's movement.
|
||||
//PlayerShoot playerShoot; // Reference to the PlayerShooting script.
|
||||
bool isDead; // Whether the player is dead.
|
||||
bool damaged; // True when the player gets damaged.
|
||||
|
||||
|
||||
void Awake ()
|
||||
{
|
||||
// Setting up the references.
|
||||
anim = GetComponent <Animator> ();
|
||||
playerAudio = GetComponent <AudioSource> ();
|
||||
playerController = GetComponent <PlayerController> ();
|
||||
//playerShooting = GetComponentInChildren <PlayerShooting> ();
|
||||
|
||||
// Set the initial health of the player.
|
||||
currentHealth = startingHealth;
|
||||
}
|
||||
|
||||
|
||||
void Update ()
|
||||
{
|
||||
// Reset the damaged flag.
|
||||
damaged = false;
|
||||
}
|
||||
|
||||
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.gameObject.CompareTag("HealthPickup"))
|
||||
{
|
||||
Debug.Log("Health Pickup");
|
||||
currentHealth = currentHealth + healthToRecoverOnPickup;
|
||||
healthSlider.value = currentHealth;
|
||||
Destroy(other);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
44
Assets/Script/Player Scripts/PlayerShoot.cs
Normal file
44
Assets/Script/Player Scripts/PlayerShoot.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class PlayerShoot: MonoBehaviour {
|
||||
|
||||
public float speed;
|
||||
public float distanceShoot=15;
|
||||
public GameObject theParent;
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
if (theParent.transform.localScale.x<=0) {
|
||||
speed *= -1;
|
||||
transform.rotation = new Quaternion (theParent.transform.rotation.x, theParent.transform.rotation.y, -90.0f, theParent.transform.rotation.w);
|
||||
} else
|
||||
transform.rotation = new Quaternion (theParent.transform.rotation.x, theParent.transform.rotation.y,90.0f, theParent.transform.rotation.w);
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
void FixedUpdate ()
|
||||
{
|
||||
transform.position = new Vector3 (transform.position.x + speed,transform.position.y,transform.position.z);
|
||||
if (transform.position.x > theParent.transform.position.x + distanceShoot || transform.position.x < theParent.transform.position.x - distanceShoot)
|
||||
DestroyObject(gameObject);
|
||||
}
|
||||
void OnTriggerEnter2D (Collider2D col)
|
||||
{
|
||||
if(col.tag == "mechant" && theParent.tag !="mechant")
|
||||
{
|
||||
print("Yeah");
|
||||
col.gameObject.GetComponent<BadGuy>().getShot();
|
||||
Destroy (gameObject);
|
||||
}
|
||||
if(col.tag == "player" && theParent.tag !="player")
|
||||
{
|
||||
print("Yeah");
|
||||
col.gameObject.GetComponent<PlayerControl>().getHurt(25);
|
||||
Destroy (gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user