@Al1kkkkkk

Почему не работает поворот по оси x в Unity?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
	[SerializeField] private float speed = 3f;
	[SerializeField] private int lives = 5;
	[SerializeField] private float jumpForce = 15f;
	private bool isGrounded = false;


	private Rigidbody2D rb;
	private Animator anim;
	private SpriteRenderer sprite;

	private States State 
	{
		get { return (States)anim.GetInteger("state"); } 
		set { anim.SetInteger("state", (int)value); } 
	}

	private void Awake() 
	{
		rb = GetComponent<Rigidbody2D>();
		anim = GetComponent<Animator>();
		sprite = GetComponentInChildren<SpriteRenderer>();
	}

	private void FixedUpdate()
	{
		CheckGround();
	}

	private void Update() {
		
		if (isGrounded) State = States.hero;
		
		if (Input.GetButton("Horizontal")) {
			Run();
		}
		if (isGrounded && Input.GetButtonDown("Jump")) {
			Jump();
		}
	}

	private void Run()
	{
		if (isGrounded)
		{
		State = States.run;
		}

		Vector3 dir = transform.right * Input.GetAxis("Horizontal");
		transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
		<b>sprite.flipX = dir.x < 0.0f; // вот эта строка не работает (но до следующего принта доходит)</b>
		print ("123");
	}

	private void Jump()
	{
		rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
	}

	private void CheckGround()
	{
		Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f);
		isGrounded = collider.Length > 1;

		if (!isGrounded) State = States.jump;
	}

}

public enum States 
{
	hero,
	run,
	jump
}
  • Вопрос задан
  • 283 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы