При старте игрок почему-то зависает в воздухе или как-будто поднимается по не видимой платформе. Также при старте между игроком и врагом какая-то невидимая дистанция. Как исправить ?
Код на игрока(заяц)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Zayatc : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
public float jumpHeight;
public Transform groundCheck;
public LayerMask whatIsGround;
public float radiusGroundCheck;
bool isGrounded;
Animator anim;
int curHP;
int MaxHP = 3;
bool isHit = false;
public Main main;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
curHP = MaxHP;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, radiusGroundCheck, whatIsGround);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
if (!isGrounded){
anim.SetInteger("State", 3);
}
if (Input.GetAxis("Horizontal") == 0 && (isGrounded))
{
anim.SetInteger("State", 1);
}
else
{ Flip();
if (isGrounded)
anim.SetInteger("State", 2);
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
}
void Flip()
{
if (Input.GetAxis("Horizontal") > 0)
transform.localRotation = Quaternion.Euler(0,0,0);
if (Input.GetAxis("Horizontal") < 0)
transform.localRotation = Quaternion.Euler(0,180,0);
}
public void RecountHP(int deltaHP)
{
curHP = curHP + deltaHP;
if (deltaHP < 0) {
StopCoroutine(OnHit());
isHit = true;
StartCoroutine(OnHit());
}
print(curHP);
if (curHP <= 0) {
GetComponent<CapsuleCollider2D>().enabled = false;
Invoke("Lose", 1.5f);
}
}
IEnumerator OnHit()
{
if (isHit)
GetComponent<SpriteRenderer>().color = new Color(1f, GetComponent<SpriteRenderer>().color.g - 0.04f, GetComponent<SpriteRenderer>().color.b - 0.04f);
else
GetComponent<SpriteRenderer>().color = new Color(1f, GetComponent<SpriteRenderer>().color.g + 0.04f, GetComponent<SpriteRenderer>().color.b + 0.04f);
if (GetComponent<SpriteRenderer>().color.g == 1f)
StopCoroutine(OnHit());
if (GetComponent<SpriteRenderer>().color.g <= 0)
isHit = false;
yield return new WaitForSeconds(0.02f);
StartCoroutine(OnHit());
}
void Lose()
{
main.GetComponent<Main>().Lose();
}
}
Код на врага
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class red_enemy_patrol : MonoBehaviour
{
public float speed = 3f;
public bool MoveLeft = true;
public Transform groundDetect;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetect.position, Vector2.down, 2f);
if (groundInfo.collider == false){
if (MoveLeft == true){
transform.eulerAngles = new Vector3(0, 0, 0);
MoveLeft = false;
}
else{
transform.eulerAngles = new Vector3(0, -180, 0);
MoveLeft = true;
}
}
}
}