using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class character : Unit
{
[SerializeField]
private float speed = 3.0f;
[SerializeField]
private int lives = 5;
[SerializeField]
private float jumpForce = 15.0f;
private bool isground = false;
private bool bullet;
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
public enum CharState
{
idle,
run,
jump
}
private CharState State
{
get { return (CharState)animator.GetInteger("state"); }
set { animator.SetInteger("state",(int)value); }
}
private void FixedUpdate()
{
checkground();
}
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
bullet = Resources.Load<Bullet>("bullet");
}
private void Update()
{
if(isground)State = CharState.idle;
if (Input.GetButton("Horizontal")) Run();
if (isground&&Input.GetButtonDown("Jump")) jump();
}
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction ,speed*Time.deltaTime);
sprite.flipX = direction.x < 0.0f;
if(isground) State = CharState.run;
}
private void jump()
{
rigidbody.AddForce(transform.up*jumpForce,ForceMode2D.Impulse);
}
private void checkground()
{
Collider2D[]colliders = Physics2D.OverlapCircleAll(transform.position,0.3f);
isground = colliders.Length > 1;
if (!isground) State = CharState.jump;
}
private void shoot()
{
Vector3 position = transform.position; position.y += 1.0f;
Instantiate(bullet, position,bullet.transform.rotation);\\тут это говно
}
}