,using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//movement
public float speed;
public float jump;
float moveVelocity;
bool grounded = false;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
public Rigidbody2D rb;
private bool facingRight;
private float movementSpeed;
public Vector3 respawnPoint;
public int curHealth;
public int maxHealth = 5;
void Start()
{
rb = GetComponent<Rigidbody2D>();
facingRight = true;
movementSpeed = 4;
curHealth = maxHealth;
}
// Update is called once per frame
void Update()
{
// Jump
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(
GetComponent<Rigidbody2D>().velocity.y, jump);
}
if (Input.GetKeyDown(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(
GetComponent<Rigidbody2D>().velocity.x, movementSpeed);
}
if (curHealth > maxHealth)
{
curHealth = maxHealth;
}
if (curHealth <= 0)
{
Die();
}
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
HandleMovement(horizontal);
Flip(horizontal);
}
private void HandleMovement(float horizontal)
{
rb.velocity = new Vector2(horizontal * movementSpeed, rb.velocity.y);
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Quaternion rotation;
void Awake()
{
rotation = transform.rotation;
}
void LateUpdate()
{
transform.rotation = rotation;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "FallDetector")
{
transform.position = respawnPoint;
}
if (other.tag == "Checkpoint")
{
respawnPoint = other.transform.position;
}
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
public void Damage(int dmg)
{
curHealth -= dmg;
}
}