107 lines
3 KiB
C#
107 lines
3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour{
|
|
|
|
public bool shooting;
|
|
public float shootingSpeed;
|
|
public GameObject player;
|
|
public float speed;
|
|
public float maxSpeed;
|
|
public float maxJump;
|
|
public bool grounded;
|
|
public CircleCollider2D normalCol;
|
|
public CircleCollider2D healingCol;
|
|
|
|
bool jump;
|
|
bool twojump;
|
|
bool threejump;
|
|
bool fourjump;
|
|
Vector3 difference;
|
|
Aim aim;
|
|
Rigidbody2D rb2d;
|
|
HealthBar health;
|
|
|
|
// Start is called before the first frame update
|
|
void Start(){
|
|
rb2d = GetComponent<Rigidbody2D>();
|
|
aim = GetComponent<Aim>();
|
|
health = GameObject.FindGameObjectWithTag("Health").GetComponent<HealthBar>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
if (Input.GetMouseButtonDown(0)){
|
|
if (jump == true){
|
|
shooting = true;
|
|
rb2d.AddForce(-difference * speed, ForceMode2D.Impulse);
|
|
jump = false;
|
|
}
|
|
if (grounded)
|
|
{
|
|
jump = true;
|
|
twojump = true;
|
|
threejump = true;
|
|
fourjump = true;
|
|
}
|
|
else if (fourjump)
|
|
{
|
|
jump = true;
|
|
twojump = true;
|
|
threejump = true;
|
|
fourjump = false;
|
|
}
|
|
else if (threejump)
|
|
{
|
|
jump = true;
|
|
twojump = true;
|
|
threejump = false;
|
|
fourjump = false;
|
|
}
|
|
else if (twojump)
|
|
{
|
|
jump = true;
|
|
twojump = false;
|
|
threejump = false;
|
|
fourjump = false;
|
|
}
|
|
}
|
|
if (grounded){
|
|
float fixedSpeed = rb2d.velocity.x;
|
|
fixedSpeed *= 0.95f;
|
|
rb2d.velocity = new Vector2(fixedSpeed, rb2d.velocity.y);
|
|
}
|
|
if (Input.GetMouseButtonUp(0)){
|
|
shooting = false;
|
|
}
|
|
difference = aim.difference;
|
|
if (rb2d.velocity.x > maxSpeed){
|
|
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
|
|
}
|
|
if (rb2d.velocity.x < -maxSpeed){
|
|
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
|
|
}
|
|
if (rb2d.velocity.y > maxJump){
|
|
rb2d.velocity = new Vector2(rb2d.velocity.x, maxJump);
|
|
}
|
|
if (rb2d.velocity.y < -maxJump){
|
|
rb2d.velocity = new Vector2(rb2d.velocity.x, -maxJump);
|
|
}
|
|
}
|
|
private void OnCollisionEnter2D(Collision2D collision){
|
|
if(collision.gameObject.tag == "Killer"){
|
|
health.hp = 0f;
|
|
}
|
|
if(collision.gameObject.tag == "Enemy"){
|
|
normalCol.enabled = false;
|
|
healingCol.enabled = true;
|
|
StartCoroutine(IsHealing(3f));
|
|
}
|
|
}
|
|
IEnumerator IsHealing (float time){
|
|
yield return new WaitForSeconds(time);
|
|
healingCol.enabled = false;
|
|
normalCol.enabled = true;
|
|
}
|
|
}
|