ПОЖАЛУЙСТА, ПОДОЖДИТЕ.
Идёт загрузка сайта...
Идёт загрузка сайта...


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
public Rigidbody2D rb;
public Animator anim;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
GroundCheckRadius = GroundCheck.GetComponent<CircleCollider2D>().radius;
}
void Update()
{
Reflect();
Jump();
Walk();
CheckingGround();
CheckingLadder();
LaddersMechanics();
//LadderUpDown();
LADDERS();
CorrectLadder();
LadderJump();
LeaveLadder();
}
public Vector2 moveVector;
public int speed = 3;
void Walk()
{
if (!blockForAnim)
{
moveVector.x = Input.GetAxisRaw("Horizontal");
if (!onLadder && !blockMoveXforJump)
{ rb.velocity = new Vector2(moveVector.x * speed, rb.velocity.y); }
}
else { moveVector.x = 0; }
anim.SetFloat("moveX", Mathf.Abs(moveVector.x));
}
// Отражение спрайтов
public bool faceRight = true;
void Reflect()
{
if (!blockMoveXforJump)
{
if ((moveVector.x > 0 && !faceRight) || (moveVector.x < 0 && faceRight))
{
//transform.Rotate(0, 180, 0);
Vector3 temp = transform.localScale;
temp.x *= -1;
transform.localScale = temp;
faceRight = !faceRight;
}
}
}
// Прыжок с земли
private bool blockJump;
public int jumpForce = 10;
void Jump()
{
if (onGround && !onLadder)
{
if (Input.GetKeyDown(KeyCode.Space))
{
anim.StopPlayback();
anim.Play("jump");
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}
// Проверка земли
public bool onGround;
public LayerMask Ground;
public Transform GroundCheck;
private float GroundCheckRadius;
public float GCRadius = 0.2f;
void CheckingGround()
{
onGround = Physics2D.OverlapCircle(GroundCheck.position, GCRadius, Ground);
anim.SetBool("onGround", onGround);
}
public float check_RADIUS = 0.04f;
public Transform ClimbLadderReady;
private void OnDrawGizmos()
{
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(CHECK_Ladder.position, check_RADIUS);
Gizmos.color = Color.blue;
Gizmos.DrawSphere(bottom_Ladder.position, check_RADIUS);
Gizmos.color = Color.gray;
Gizmos.DrawWireSphere(GroundCheck.position, GCRadius);
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(Foot_Level.position, Foot_Level_RADIUS);
Gizmos.color = Color.yellow;
Gizmos.DrawLine
(
Foot_Level.position,
new Vector2(Foot_Level.position.x, Foot_Level.position.y + rayStairsDistance)
);
Gizmos.color = Color.magenta;
Gizmos.DrawLine
(
Foot_Level.position,
new Vector2(Foot_Level.position.x, Foot_Level.position.y - rayStairsDistance)
);
Gizmos.color = Color.green;
Gizmos.DrawSphere(ClimbLadderReady.position, check_RADIUS);
}
public Transform CHECK_Ladder;
public bool checkedLadder;
public LayerMask LadderMask;
public Transform bottom_Ladder;
public bool bottomCheckedLadder;
void CheckingLadder()
{
checkedLadder = Physics2D.OverlapPoint(CHECK_Ladder.position, LadderMask);
bottomCheckedLadder = Physics2D.OverlapPoint(bottom_Ladder.position, LadderMask);
checkClimbLadder = Physics2D.OverlapPoint(ClimbLadderReady.position, LadderMask);
}
public float ladderSpeed = 1.5f;
void LaddersMechanics()
{
if (onLadder)
{
rb.bodyType = RigidbodyType2D.Kinematic;
//rb.velocity = new Vector2(rb.velocity.x, moveVector.y * ladderSpeed);
}
else { rb.bodyType = RigidbodyType2D.Dynamic; }
}
//void LadderUpDown()
//{
// moveVector.y = Input.GetAxisRaw("Vertical");
// anim.SetFloat("moveY", moveVector.y);
// anim.SetInteger("upDown", (int)moveVector.y);
//}
float vertInput;
public bool onLadder;
void LADDERS()
{
vertInput = Input.GetAxisRaw("Vertical");
if (!blockForAnim)
{
if (checkedLadder || bottomCheckedLadder)
{
if (!checkedLadder && bottomCheckedLadder) // ПЕРС СВЕРХУ
{
if (vertInput < 0) { onLadder = true; anim.Play("Climb_Down"); }
else if (onLadder) { onLadder = false; anim.Play("idle"); }
}
else if (checkedLadder && bottomCheckedLadder) // НА ЛЕСТНИЦЕ
{
if (vertInput > 0)
{
if (checkClimbLadder) { onLadder = true; anim.Play(WhatFootNext("Up")); }
else { ClimbUpSwitch(); }
}
else if (vertInput < 0) { onLadder = true; anim.Play(WhatFootNext("Down")); }
}
else if (checkedLadder && !bottomCheckedLadder) // ПЕРС СНИЗУ
{
if (vertInput > 0) { onLadder = true; anim.Play(WhatFootNext("Up")); }
else if (onLadder) { onLadder = false; anim.Play("idle"); }
}
}
else { onLadder = false; } // ВНЕ ЛЕСТНИЦЫ
if (vertInput != 0) { moveVector.x = 0; }
}
LaddersMechanics();
anim.SetBool("onLadder", onLadder);
anim.SetInteger("upDown", (int)vertInput);
}
bool checkClimbLadder;
void ClimbUpSwitch()
{
if (onLadder) { anim.Play("Climb_Up"); }
else
{
blockForAnim = true;
moveVector.x = 0;
rb.velocity = new Vector2(0, rb.velocity.y);
ClimbLadderReady.gameObject.SetActive(true);
}
}
bool leftFoot = true;
string WhatFootNext(string direction)
{
leftFoot = !leftFoot;
if (direction == "Up")
{
if (leftFoot) { return "LeftUp"; }
else { return "RightUp"; }
}
else
{
if (leftFoot) { return "LeftDown"; }
else { return "RightDown"; }
}
}
bool corrected = true;
void CorrectLadder()
{
if (onLadder && corrected)
{
corrected = !corrected;
rb.velocity = Vector2.zero;
LadderCenter();
if (checkedLadder && bottomCheckedLadder) { LadderStaisCorrect(); }
}
else if (!onLadder && !corrected) { corrected = true; }
}
float ladderCenter;
void LadderCenter()
{
if (checkedLadder)
{
ladderCenter = Physics2D.OverlapPoint
(
CHECK_Ladder.position,
LadderMask
).GetComponent<BoxCollider2D>().bounds.center.x;
}
else if (bottomCheckedLadder)
{
ladderCenter = Physics2D.OverlapPoint
(
bottom_Ladder.position,
LadderMask
).GetComponent<BoxCollider2D>().bounds.center.x;
}
transform.position = new Vector2(ladderCenter, transform.position.y);
}
float distanceUpStair;
float distanceDownStair;
float distanceForStairCorrect;
public LayerMask LadderTriggerStairsMask;
public Transform Foot_Level;
public float Foot_Level_RADIUS = 0.04f;
public float rayStairsDistance = 0.9f;
void LadderStaisCorrect()
{
distanceUpStair = Physics2D.Raycast
(
Foot_Level.position,
Vector2.up,
rayStairsDistance,
LadderTriggerStairsMask
).distance;
distanceDownStair = Physics2D.Raycast
(
Foot_Level.position,
Vector2.down,
rayStairsDistance,
LadderTriggerStairsMask
).distance;
//Debug.Log("До верхней: " + distanceUpStair);
//Debug.Log("До нижней: " + distanceDownStair);
if (distanceUpStair != 0 && distanceDownStair != 0)
{
if (distanceDownStair <= distanceUpStair)
{
distanceForStairCorrect = -1 * distanceDownStair;
}
else { distanceForStairCorrect = distanceUpStair; }
}
else if (distanceUpStair == 0) { distanceForStairCorrect = -1 * distanceDownStair; }
else if (distanceDownStair == 0) { distanceForStairCorrect = distanceUpStair; }
//Debug.Log("В итоге: " + distanceForStairCorrect);
transform.position =
new Vector2(transform.position.x, transform.position.y + distanceForStairCorrect);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "LadderStairs")
{
Physics2D.IgnoreCollision
(
collision.GetComponent<EdgeCollider2D>(),
GetComponent<CapsuleCollider2D>(),
true
);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "LadderStairs")
{
Physics2D.IgnoreCollision
(
collision.GetComponent<EdgeCollider2D>(),
GetComponent<CapsuleCollider2D>(),
false
);
}
}
private bool blockMoveXforJump;
public float jumpLadderTime = 0.5f;
private float timerJumpLadder;
public Vector2 jumpAngle = new Vector2(3.5f, 5);
void LadderJump()
{
if (onLadder && Mathf.Abs(moveVector.x) > 0 && Input.GetKeyDown(KeyCode.Space))
{
blockMoveXforJump = true;
moveVector.x = 0;
onLadder = false;
anim.StopPlayback();
anim.Play("jump");
LaddersMechanics();
IgnoreObjectForJump(true);
rb.velocity = new Vector2(0, 0);
rb.velocity = new Vector2(transform.localScale.x * jumpAngle.x, jumpAngle.y);
}
else if (onLadder && Input.GetKeyDown(KeyCode.Space) && !blockForAnim)
{
onLadder = false;
if (CheckStepLevel()) { anim.Play("idle"); }
else { anim.Play("fall"); }
}
if (blockMoveXforJump)
{
if ((timerJumpLadder += Time.deltaTime) >= jumpLadderTime)
{
if (onLadder || onGround || Input.GetAxisRaw("Horizontal") != 0)
{
blockMoveXforJump = false;
timerJumpLadder = 0;
IgnoreObjectForJump(false);
}
}
}
}
public bool blockForAnim = false;
public float stairsDistance = 0.88f;
void StartAnim()
{
blockForAnim = true;
if (vertInput > 0)
{
transform.position =
new Vector2(transform.position.x, transform.position.y + stairsDistance);
}
else if (vertInput < 0)
{
transform.position =
new Vector2(transform.position.x, transform.position.y - stairsDistance);
}
}
void FinishAnim()
{
blockForAnim = false;
}
// Проверка на коллизию с землёй -------------------------------
private void OnCollisionEnter2D(Collision2D collision)
{
if ((Ground.value & 1 << collision.gameObject.layer) != 0)
{ anim.SetBool("groundCollision", true); }
}
private void OnCollisionExit2D(Collision2D collision)
{
if ((Ground.value & 1 << collision.gameObject.layer) != 0)
{ anim.SetBool("groundCollision", false); }
}
// Проверка на коллизию с землёй -------------------------------
bool CheckStepLevel()
{
Collider2D coll;
if (coll = Physics2D.OverlapCircle(Foot_Level.position, Foot_Level_RADIUS, Ground))
{
float topY = Foot_Level.position.y + Foot_Level_RADIUS;
float bottomY = Foot_Level.position.y - Foot_Level_RADIUS;
if (coll.gameObject.transform.position.y > bottomY && transform.position.y < topY)
{
return true;
}
else { return false; }
}
else { return false; }
}
void LeaveLadder()
{
if (onLadder && Mathf.Abs(moveVector.x) > 0 && onGround)
{
if (CheckStepLevel())
{
onLadder = false;
anim.SetBool("onLadder", onLadder);
anim.Play("walk");
}
}
}
public float distanceClimb = 1.94f;
void ClimbLadderUp()
{
blockForAnim = true;
transform.position = new Vector2(transform.position.x, transform.position.y + distanceClimb);
}
void ClimbLadderDown()
{
blockForAnim = true;
transform.position = new Vector2(transform.position.x, transform.position.y - distanceClimb);
}
public Transform IgnoreForJump;
void IgnoreObjectForJump(bool ignore)
{
//if (ignore) { IgnoreForJump.gameObject.SetActive(true); }
//else { IgnoreForJump.gameObject.SetActive(false); }
IgnoreForJump.gameObject.SetActive(ignore);
}
public void MonitorIgnore(Collider2D coll, bool OnOff)
{
if (coll.gameObject.layer == 10)
{
Physics2D.IgnoreCollision(GetComponent<CapsuleCollider2D>(), coll, OnOff);
}
}
}
Скандинавская мифология — мифология древних скандинавов, часть древнегерманской мифологии.
Скандинавская мифология — мифология древних скандинавов, часть древнегерманской мифологии.
Скандинавская мифология — мифология древних скандинавов, часть древнегерманской мифологии.
Скандинавская мифология — мифология древних скандинавов, часть древнегерманской мифологии.
Скандинавская мифология — мифология древних скандинавов, часть древнегерманской мифологии.