Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
public class TheCharacterController : Unit {
[SerializeField]
private int lives = 3;
public float controller_speed = 5.0f;
[SerializeField]
private float jumpForce = 12.0f;
private float circleArea = 0.5f;
private bool isGround = false;
//public bool isAttackCheck = false;
private Rigidbody2D rigid;
private Animator animator;
private SpriteRenderer sprite;
public Transform hand_kik ;
public GameObject character_hand;
public float hand_kik_radius = 0.5f;
bool hand_direction = false;
public Bullet bullet;
public LayerMask layerEnemy = 10;
private CharState State{
get{return (CharState)animator.GetInteger ("State"); }
set{ animator.SetInteger ("State", (int)value);}
}
private Vector3 direction = new Vector3();
void Awake (){
rigid = GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator> ();
sprite = GetComponentInChildren<SpriteRenderer> ();
}
void Start (){
//isAttackCheck = false;
}
void Update () {
if(isGround)State = CharState.Idle;
if (Input.GetButton ("Horizontal")) {
Run ();
}
if (Input.GetButtonDown ("Fire2")) {
Shoot ();
}
if (Input.GetButtonDown ("Fire1")) {
UnitAttack.Action (hand_kik.position , hand_kik_radius, 10, 100.0f, false);
State = CharState.Attack;
}
if (isGround && Input.GetButtonDown ("Jump")) {
Jump ();
}
}
void FixedUpdate(){
ChkGround ();
}
public override void ReceiveDamage () // переопределенный метод определяющий урон по игроку
{
lives--;
rigid.velocity = Vector3.zero;
rigid.AddForce ((transform.up * 20.0f) + (direction * 7.0f), ForceMode2D.Impulse);
Debug.Log (lives);
}
public void Run (){
Vector3 handlocalVector = new Vector3 (hand_kik.localPosition.x * -1.0f, hand_kik.localPosition.y); // отвечает за дочерний объект hand
bool isCheckHandLocalVector = handlocalVector.x < 0.0f;
direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards (transform.position, transform.position + direction, controller_speed * Time.deltaTime);
if (direction.x < 0.0f ) {
sprite.flipX = true;
} else {
sprite.flipX = false;
}
if (direction.x < 0.0f && isCheckHandLocalVector) {
hand_kik.localPosition = handlocalVector;
} else if (direction.x >= 0.0f && !isCheckHandLocalVector) {
hand_kik.localPosition = handlocalVector;
} else if (direction.x >= 0.0f && isCheckHandLocalVector) {
isCheckHandLocalVector = false;
}else if (direction.x < 0.0f && !isCheckHandLocalVector) {
isCheckHandLocalVector = true;
}
if(isGround)State = CharState.Run;
}
private void ChkGround (){
Collider2D[] collaiders = Physics2D.OverlapCircleAll (transform.position, circleArea);
bool isEnimyDown = collaiders.All (x => !x.GetComponent<Enimes> ()); // переменная которая определяет есть ли коллайдер врага под персонажем, (использование лямда выражений)
isGround = collaiders.Length > 1 && isEnimyDown;
if (!isGround ) {
State = CharState.Jump;
}
}
public void Jump (){
rigid.AddForce (transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Shoot (){
Vector3 bullet_pos = transform.position;
bullet_pos.y += 1.5f;
Bullet newBullet = Instantiate (bullet, bullet_pos, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * direction.x;
}
public void OnTriggerEnter2D (Collider2D collider){
Unit unit = collider.gameObject.GetComponent<Unit> ();
if (unit) {
ReceiveDamage ();
}
}
}
public enum CharState {
Idle,Run,Jump,Attack
}
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
public class TheCharacterController : Unit {
[SerializeField]
private int lives = 3;
public float controller_speed = 5.0f;
[SerializeField]
private float jumpForce = 12.0f;
private float circleArea = 0.5f;
private bool isGround = false;
//public bool isAttackCheck = false;
private Rigidbody2D rigid;
private Animator animator;
private SpriteRenderer sprite;
public Transform hand_kik ;
public GameObject character_hand;
public float hand_kik_radius = 0.5f;
bool hand_direction = false;
public Bullet bullet;
public LayerMask layerEnemy = 10;
private CharState State{
get{return (CharState)animator.GetInteger ("State"); }
set{ animator.SetInteger ("State", (int)value);}
}
private Vector3 direction = new Vector3();
void Awake (){
rigid = GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator> ();
sprite = GetComponentInChildren<SpriteRenderer> ();
}
void Start (){
//isAttackCheck = false;
}
void Update () {
if(isGround)State = CharState.Idle;
if (Input.GetButton ("Horizontal")) {
Run ();
}
if (Input.GetButtonDown ("Fire2")) {
Shoot ();
}
if (Input.GetButtonDown ("Fire1")) {
UnitAttack.Action (hand_kik.position , hand_kik_radius, 10, 100.0f, false);
State = CharState.Attack;
}
if (isGround && Input.GetButtonDown ("Jump")) {
Jump ();
}
}
void FixedUpdate(){
ChkGround ();
}
public override void ReceiveDamage () // переопределенный метод определяющий урон по игроку
{
lives--;
rigid.velocity = Vector3.zero;
rigid.AddForce ((transform.up * 20.0f) + (direction * 7.0f), ForceMode2D.Impulse);
Debug.Log (lives);
}
public void Run (){
Vector3 handlocalVector = new Vector3 (hand_kik.localPosition.x * -1.0f, hand_kik.localPosition.y); // отвечает за дочерний объект hand
bool isCheckHandLocalVector = handlocalVector.x < 0.0f;
direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards (transform.position, transform.position + direction, controller_speed * Time.deltaTime);
if (direction.x < 0.0f ) {
sprite.flipX = true;
} else {
sprite.flipX = false;
}
if (direction.x < 0.0f && isCheckHandLocalVector) {
hand_kik.localPosition = handlocalVector;
} else if (direction.x >= 0.0f && !isCheckHandLocalVector) {
hand_kik.localPosition = handlocalVector;
} else if (direction.x >= 0.0f && isCheckHandLocalVector) {
isCheckHandLocalVector = false;
}else if (direction.x < 0.0f && !isCheckHandLocalVector) {
isCheckHandLocalVector = true;
}
if(isGround)State = CharState.Run;
}
private void ChkGround (){
Collider2D[] collaiders = Physics2D.OverlapCircleAll (transform.position, circleArea);
bool isEnimyDown = collaiders.All (x => !x.GetComponent<Enimes> ()); // переменная которая определяет есть ли коллайдер врага под персонажем, (использование лямда выражений)
isGround = collaiders.Length > 1 && isEnimyDown;
if (!isGround ) {
State = CharState.Jump;
}
}
public void Jump (){
rigid.AddForce (transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Shoot (){
Vector3 bullet_pos = transform.position;
bullet_pos.y += 1.5f;
Bullet newBullet = Instantiate (bullet, bullet_pos, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * direction.x;
}
public void OnTriggerEnter2D (Collider2D collider){
Unit unit = collider.gameObject.GetComponent<Unit> ();
if (unit) {
ReceiveDamage ();
}
}
}
public enum CharState {
Idle,Run,Jump,Attack
}
вот скрин контроллера(анимация New Animation)
Скрытый текст: