Spoiler (click to show/hide):
- Code: Select All Code
using UnityEngine;
using System.Collections;
public class Player : Character
{
private static Player instance;
public static Player Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<Player>();
}
return instance;
}
}
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public Rigidbody2D MyRigidbody { get; set; }
public bool Attack { get; set; }
public bool Slide { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
private Vector2 startPos;
// Use this for initialization
public override void Start()
{
Debug.Log("PlayerStart");
base.Start();
startPos = transform.position;
MyRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
OnGround = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandLayers();
}
private void HandleMovement(float horizontal)
{
if (MyRigidbody.velocity.y < 0)
{
myAnimator.SetBool("Land", true);
}
if(!Attack && !Slide && (OnGround || airControl))
{
MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
}
if (Jump && MyRigidbody.velocity.y == 0)
{
MyRigidbody.AddForce(new Vector2(0, jumpForce));
}
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
myAnimator.SetTrigger("Jump");
}
if (Input.GetKeyDown(KeyCode.Z))
{
myAnimator.SetTrigger("Attack");
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
myAnimator.SetTrigger("Slide");
}
if (Input.GetKeyDown(KeyCode.X))
{
myAnimator.SetTrigger("Throw");
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
ChangeDirection();
}
}
private bool IsGrounded()
{
if (MyRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandLayers()
{
if (!OnGround)
{
myAnimator.SetLayerWeight(1, 1);
}
else
{
myAnimator.SetLayerWeight(1, 0);
}
}
public override void ThrowKnife(int value)
{
if (!OnGround && value == 1 || OnGround && value == 0)
{
base.ThrowKnife(value);
}
}
}