using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class PlayerControler : MonoBehaviour {
|
|
|
|
|
|
public Animator animator;
|
|
// Use this for initialization
|
|
void Start () {
|
|
}
|
|
|
|
public float speed;
|
|
public Vector2 jumpVector;
|
|
public int health = 0;
|
|
private int displayedHealth = 0;
|
|
private bool updateHealth = false;
|
|
public GameObject bullet;
|
|
public GameObject heart;
|
|
bool canJump = true;
|
|
|
|
|
|
|
|
void Update() {
|
|
Vector2 velo = rigidbody2D.velocity;
|
|
velo.x = Input.GetAxis ("Horizontal_P1") * speed;
|
|
rigidbody2D.velocity = velo;
|
|
animator.SetFloat("Velocity",velo.magnitude);
|
|
animator.SetBool ("Fall", !canJump);
|
|
applyPlayerDirection (Input.GetAxisRaw ("Horizontal_P1"));
|
|
|
|
if (displayedHealth != health) {
|
|
updateHealth = true;
|
|
}
|
|
if (updateHealth)
|
|
displayHealth (health);
|
|
|
|
|
|
if (Input.GetKey (KeyCode.UpArrow)){
|
|
if (canJump == true){
|
|
rigidbody2D.velocity = jumpVector;
|
|
canJump = false;
|
|
}
|
|
}
|
|
|
|
//if (Input.GetKey (KeyCode.Space)){
|
|
//shoot
|
|
//GameObject bulletGO = Instantiate (bullet) as GameObject;
|
|
//bulletGO.transform.position = transform.position;
|
|
//}
|
|
}
|
|
|
|
private void displayHealth(int health){
|
|
GameObject[] hearts = GameObject.FindGameObjectsWithTag("heartUI2");
|
|
foreach (GameObject desHeart in hearts)
|
|
GameObject.Destroy (desHeart);
|
|
|
|
for (int i=1; i<= health; i++) {
|
|
GameObject heartCanister = Instantiate (heart) as GameObject;
|
|
Vector3 heartPos = new Vector3 (1-(i * 0.033f), 0.95f, 0);
|
|
heartCanister.transform.position = heartPos;
|
|
}
|
|
displayedHealth = health;
|
|
updateHealth = false;
|
|
}
|
|
|
|
private void applyPlayerDirection(float moveHorizontal)
|
|
{
|
|
if (moveHorizontal != 0)
|
|
{
|
|
Vector3 scale = transform.localScale;
|
|
scale.x = -moveHorizontal * Mathf.Abs(scale.x);
|
|
transform.localScale = scale;
|
|
}
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D col){
|
|
if (col.collider.tag == "ground")
|
|
canJump = true;
|
|
else if (col.collider.tag == "Player") {
|
|
canJump = true;
|
|
if(transform.position.y>col.transform.position.y){
|
|
col.gameObject.GetComponent<player2_controls>().health --;
|
|
//health --;
|
|
rigidbody2D.velocity = jumpVector;
|
|
canJump = false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|