A
A
Al1kkkkkk2021-07-02 21:35:52
Unity
Al1kkkkkk, 2021-07-02 21:35:52

Why doesn't x rotation work in 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
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question