Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : Unit {
[SerializeField]
private float speedSlowdown;
[SerializeField]
private float speedFall;
[SerializeField]
private float damageAttackRed;
[SerializeField]
private float damageAttackSkill;
[SerializeField]
protected float damageAttackStrike;
private bool useDoubleJump = false;//использован ли второй прыжок
private float timePressedButton = 0f;
private StatePlayer State{
get{return (StatePlayer)animator.GetInteger("State");}
set{animator.SetInteger("State", (int)value);}
}
private void Start()
{
Time.timeScale = 0.05f;
}
private void Update(){
CheckGrounded();
if (!isGrounded) State = StatePlayer.Jump;
if (isGrounded)
{
State = StatePlayer.Idle;
useDoubleJump = false;
}
TimePressedButton();
//if (Input.anyKey)
// SpeedupTime();
//else
SlowTime();
if (Input.GetButton("Horizontal"))
Run();
if (Input.GetButtonDown("Jump"))
Jump();
if (Input.GetButtonDown("Fire1"))
Attack();
if (Input.GetButton("Vertical"))
SpeedFall();
}
//время нажатия на клавишу
private void TimePressedButton()
{
if (Input.anyKey)
timePressedButton += speedSlowdown / 100f;
else timePressedButton = 0f;
}
//максимальное значение timeScale в зависимости от продолжительности нажатия клавиши
private float MaxTimeScale()
{
float maxTimeScale = timePressedButton;
if (maxTimeScale > 1f)
return 1f;
return maxTimeScale;
}
//замедляем течене времени, то есть уменьшаем timeScale
private void SlowTime()
{
float curTimeScale = Time.timeScale - speedSlowdown * 2f;
if (curTimeScale > 0.05f)
Time.timeScale = curTimeScale;
else Time.timeScale = 0.05f;
}
//ускоряем течение времени, то есть увеличиваем timeScale
private void SpeedupTime()
{
if (Time.timeScale < MaxTimeScale())
Time.timeScale += speedSlowdown;
else Time.timeScale = 1f;
}
//ускоряем падение
private void SpeedFall()
{
if (isGrounded)
return;
if (Input.GetAxis("Vertical") > 0)
return;
Vector2 force = new Vector2(0, -speedFall);
if (rg.velocity.y > 0)
force *= 2f;
rg.AddForce(force, ForceMode2D.Impulse);
}
private void Run()
{
Vector3 direction = Vector3.right * Input.GetAxis("Horizontal");
transform.position = Vector2.MoveTowards(transform.position, transform.position + direction, Time.deltaTime * speed);
render.flipX = direction.x < 0f;
State = StatePlayer.Run;
}
private void Jump()
{
if (isGrounded){
rg.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
return;
}
if (!useDoubleJump){
Vector2 force = transform.up * jumpForce;
/* сопоставляем второй прыжок по высоте первому
если игрок падает то прыжок все равно ниже, если вверх то чучуть выше*/
if (rg.velocity.y < 0f)
rg.velocity = Vector2.down;
if (rg.velocity.y > 1.5f)
rg.velocity = Vector2.up;
rg.AddForce(force, ForceMode2D.Impulse);
useDoubleJump = true;
}
}
private void Attack()
{
if (isGrounded) State = StatePlayer.Attack;
else State = StatePlayer.JumpAttack;
//опредляем в какой стороне проверять урон по врагам, в зависимости от поворота героя
Vector3 positionWeapon = render.flipX ? transform.position + new Vector3(-1, 1, 0) : transform.position + new Vector3(1, 1, 0);
Vector2 sizeWeapon = new Vector2(1.2f, 0.2f);
Collider2D[] colliders = Physics2D.OverlapBoxAll(positionWeapon, sizeWeapon, 0);
if (colliders.Length > 1){
foreach (Collider2D collider in colliders){
Unit unit = collider.GetComponent<Unit>();
if (unit && !(unit is PlayerController))
unit.ReceiveDamage(damageAttack);
}
}
}
public override void ReceiveDamage(float damage)
{
State = StatePlayer.Hurt;
base.ReceiveDamage(damage);
}
}
enum StatePlayer{
Idle,
Run,
Jump,
IdleSword,
Attack,
RedAttack,
JumpAttack,
Strike,
Skill,
JumpSkill,
Hurt,
Hurt2,
Crouch,
Crouchdash
}
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : Unit {
[SerializeField]
private float speedSlowdown;
[SerializeField]
private float speedFall;
[SerializeField]
private float damageAttackRed;
[SerializeField]
private float damageAttackSkill;
[SerializeField]
protected float damageAttackStrike;
private bool useDoubleJump = false;//использован ли второй прыжок
private float timePressedButton = 0f;
private StatePlayer State{
get{return (StatePlayer)animator.GetInteger("State");}
set{animator.SetInteger("State", (int)value);}
}
private void Start()
{
Time.timeScale = 0.05f;
}
private void Update(){
CheckGrounded();
if (!isGrounded) State = StatePlayer.Jump;
if (isGrounded)
{
State = StatePlayer.Idle;
useDoubleJump = false;
}
TimePressedButton();
//if (Input.anyKey)
// SpeedupTime();
//else
SlowTime();
if (Input.GetButton("Horizontal"))
Run();
if (Input.GetButtonDown("Jump"))
Jump();
if (Input.GetButtonDown("Fire1"))
Attack();
if (Input.GetButton("Vertical"))
SpeedFall();
}
//время нажатия на клавишу
private void TimePressedButton()
{
if (Input.anyKey)
timePressedButton += speedSlowdown / 100f;
else timePressedButton = 0f;
}
//максимальное значение timeScale в зависимости от продолжительности нажатия клавиши
private float MaxTimeScale()
{
float maxTimeScale = timePressedButton;
if (maxTimeScale > 1f)
return 1f;
return maxTimeScale;
}
//замедляем течене времени, то есть уменьшаем timeScale
private void SlowTime()
{
float curTimeScale = Time.timeScale - speedSlowdown * 2f;
if (curTimeScale > 0.05f)
Time.timeScale = curTimeScale;
else Time.timeScale = 0.05f;
}
//ускоряем течение времени, то есть увеличиваем timeScale
private void SpeedupTime()
{
if (Time.timeScale < MaxTimeScale())
Time.timeScale += speedSlowdown;
else Time.timeScale = 1f;
}
//ускоряем падение
private void SpeedFall()
{
if (isGrounded)
return;
if (Input.GetAxis("Vertical") > 0)
return;
Vector2 force = new Vector2(0, -speedFall);
if (rg.velocity.y > 0)
force *= 2f;
rg.AddForce(force, ForceMode2D.Impulse);
}
private void Run()
{
Vector3 direction = Vector3.right * Input.GetAxis("Horizontal");
transform.position = Vector2.MoveTowards(transform.position, transform.position + direction, Time.deltaTime * speed);
render.flipX = direction.x < 0f;
State = StatePlayer.Run;
}
private void Jump()
{
if (isGrounded){
rg.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
return;
}
if (!useDoubleJump){
Vector2 force = transform.up * jumpForce;
/* сопоставляем второй прыжок по высоте первому
если игрок падает то прыжок все равно ниже, если вверх то чучуть выше*/
if (rg.velocity.y < 0f)
rg.velocity = Vector2.down;
if (rg.velocity.y > 1.5f)
rg.velocity = Vector2.up;
rg.AddForce(force, ForceMode2D.Impulse);
useDoubleJump = true;
}
}
private void Attack()
{
if (isGrounded) State = StatePlayer.Attack;
else State = StatePlayer.JumpAttack;
//опредляем в какой стороне проверять урон по врагам, в зависимости от поворота героя
Vector3 positionWeapon = render.flipX ? transform.position + new Vector3(-1, 1, 0) : transform.position + new Vector3(1, 1, 0);
Vector2 sizeWeapon = new Vector2(1.2f, 0.2f);
Collider2D[] colliders = Physics2D.OverlapBoxAll(positionWeapon, sizeWeapon, 0);
if (colliders.Length > 1){
foreach (Collider2D collider in colliders){
Unit unit = collider.GetComponent<Unit>();
if (unit && !(unit is PlayerController))
unit.ReceiveDamage(damageAttack);
}
}
}
public override void ReceiveDamage(float damage)
{
State = StatePlayer.Hurt;
base.ReceiveDamage(damage);
}
}
enum StatePlayer{
Idle,
Run,
Jump,
IdleSword,
Attack,
RedAttack,
JumpAttack,
Strike,
Skill,
JumpSkill,
Hurt,
Hurt2,
Crouch,
Crouchdash
}
Вот видео проблемы
[video]
https://drive.google.com/file/d/1MJbZPX ... sp=sharing
[/video]
Скрин игрока в юнити
[img]
https://drive.google.com/file/d/106uqdl ... sp=sharing
[/img]