Ребята, есть код персонажа. Делаю 2D игру, и не могли бы вы помочь немного с кодом. Он работает не так, как хотелось бы, а я не могу понять что не так.
Огромное спасибо тем, кто откликнется.
В общем, управление влево и вправо работает нормально (анимация), но когда прыгаю, то анимация ходьбы продолжается, есть анимация стрельбы, но она тоже странно работает.
И я пытался сделать приседание персонажа (2D), нужно просто брать у персонажа CapsuleCollider2D и менять Size по Y. Не могу догнать как сделать приседание и тоже анимацию, которая не будет перебиваться другой анимацией.
Вот код...
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
public GameObject Player;
public Animator anim;
Rigidbody2D rb;
public float speed, jumpPower;
public Quaternion constRotation;
public float ShootTimer = 2f;
public bool grounded = false;
public Collider2D[] groundCollisions;
public float groundCheckRadius = 0.5f;
public LayerMask groundLayer;
public Transform groundCheck;
public bool move;
private float JumpCount;
public float timeDelay = 0.2f;
float timer;
public AudioSource sourse;
public AudioClip jumpClip;
public AudioClip walkClip;
public AudioClip deadClip;
public AudioClip shootClip;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
sourse = GetComponent<AudioSource>();
anim = GetComponentInChildren<Animator>();
}
void FixedUpdate()
{
if (grounded)
{
move = true;
}
transform.rotation = constRotation;
float x = Input.GetAxis("Horizontal");
groundCollisions = Physics2D.OverlapCircleAll(groundCheck.position, groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0)
grounded = true;
else
grounded = false;
if (grounded)
{
anim.SetBool("jump", false);
}
else
{
anim.SetBool("jump", true);
}
if (x != 0)
{
if (grounded)
{
if (timer > 0)
{
timer -= Time.deltaTime;
}
if (timer <= 0) {
sourse.PlayOneShot (walkClip, 0.5f);
timer = timeDelay;
}
}
else
{
anim.SetBool("move", false);
}
if (move)
{
anim.SetBool("move", true);
rb.velocity = new Vector2(x * speed, rb.velocity.y);
}
}
else
{
anim.SetBool("move", false);
}
//JUMP
if (Input.GetButtonDown("Jump") && grounded)
{
if (JumpCount < 1)
{
Vector3 jump = new Vector3(0.0f, 700.0f, 0.0f);
sourse.PlayOneShot(jumpClip, GameManager.volumeCount);
rb.AddForce(jump);
JumpCount++;
}
}
if (Input.GetButtonDown("Crouch"))
{
float floatx1 = 0f;
float floaty1 = -.275f;
float floatx2 = .5f;
float floaty2 = .55f;
Vector2 offset = new Vector2(floatx1, -floaty1);
Vector2 size = new Vector2(floatx2, floaty2);
}
ShootTimer -= Time.deltaTime;
if (Input.GetButtonDown ("Shoot") && grounded) {
if (ShootTimer <= 0) {
anim.SetBool ("shoot", true);
sourse.PlayOneShot (shootClip, GameManager.volumeCount);
ShootTimer = 1f;
}
}
else {
anim.SetBool ("shoot", false);
}
if (x > 0)
constRotation.y = 0;
if (x < 0)
constRotation.y = 180;
}
private void OnCollisionEnter2D(Collision2D col) {
JumpCount = 0;
}
private void OnCollisionStay2D(Collision2D col)
{
if (!grounded && col.gameObject.tag == "Ground" || col.gameObject.tag == "Border" || col.gameObject.tag == "Bounce")
{
move = false;
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
public GameObject Player;
public Animator anim;
Rigidbody2D rb;
public float speed, jumpPower;
public Quaternion constRotation;
public float ShootTimer = 2f;
public bool grounded = false;
public Collider2D[] groundCollisions;
public float groundCheckRadius = 0.5f;
public LayerMask groundLayer;
public Transform groundCheck;
public bool move;
private float JumpCount;
public float timeDelay = 0.2f;
float timer;
public AudioSource sourse;
public AudioClip jumpClip;
public AudioClip walkClip;
public AudioClip deadClip;
public AudioClip shootClip;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
sourse = GetComponent<AudioSource>();
anim = GetComponentInChildren<Animator>();
}
void FixedUpdate()
{
if (grounded)
{
move = true;
}
transform.rotation = constRotation;
float x = Input.GetAxis("Horizontal");
groundCollisions = Physics2D.OverlapCircleAll(groundCheck.position, groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0)
grounded = true;
else
grounded = false;
if (grounded)
{
anim.SetBool("jump", false);
}
else
{
anim.SetBool("jump", true);
}
if (x != 0)
{
if (grounded)
{
if (timer > 0)
{
timer -= Time.deltaTime;
}
if (timer <= 0) {
sourse.PlayOneShot (walkClip, 0.5f);
timer = timeDelay;
}
}
else
{
anim.SetBool("move", false);
}
if (move)
{
anim.SetBool("move", true);
rb.velocity = new Vector2(x * speed, rb.velocity.y);
}
}
else
{
anim.SetBool("move", false);
}
//JUMP
if (Input.GetButtonDown("Jump") && grounded)
{
if (JumpCount < 1)
{
Vector3 jump = new Vector3(0.0f, 700.0f, 0.0f);
sourse.PlayOneShot(jumpClip, GameManager.volumeCount);
rb.AddForce(jump);
JumpCount++;
}
}
if (Input.GetButtonDown("Crouch"))
{
float floatx1 = 0f;
float floaty1 = -.275f;
float floatx2 = .5f;
float floaty2 = .55f;
Vector2 offset = new Vector2(floatx1, -floaty1);
Vector2 size = new Vector2(floatx2, floaty2);
}
ShootTimer -= Time.deltaTime;
if (Input.GetButtonDown ("Shoot") && grounded) {
if (ShootTimer <= 0) {
anim.SetBool ("shoot", true);
sourse.PlayOneShot (shootClip, GameManager.volumeCount);
ShootTimer = 1f;
}
}
else {
anim.SetBool ("shoot", false);
}
if (x > 0)
constRotation.y = 0;
if (x < 0)
constRotation.y = 180;
}
private void OnCollisionEnter2D(Collision2D col) {
JumpCount = 0;
}
private void OnCollisionStay2D(Collision2D col)
{
if (!grounded && col.gameObject.tag == "Ground" || col.gameObject.tag == "Border" || col.gameObject.tag == "Bounce")
{
move = false;
}
}
}