Unity Sprite Flip超出摄影机范围

问题描述

我试图在按下箭头键时翻转精灵,但是它们以某种方式从相机的末端中消失了。在我看来,整个摄像头都是翻转的,而不是精灵。解决这个问题的正确方法是什么?

用户按下向左/向右箭头键时,我试图翻转。下面是我的代码

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

public class Player : MonoBehavIoUr
{
    public float speed;
    public float input;
    Rigidbody2D rb;
    Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void Update () 
    {
        if (input != 0) {
            print("is running");
            anim.SetBool("isRunning",true);
        } else {
            print("is not running");
            anim.SetBool("isRunning",false);
        }

    if (input > 0) {
            transform.eulerAngles = new Vector3(0,0);
        }else if (input < 0){
            transform.eulerAngles = new Vector3(0,180,0);
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        input = Input.GetAxis("Horizontal");
        rb.veLocity = new Vector2(input * speed,rb.veLocity.y);
    }
}

这是场景/游戏的屏幕截图

enter image description here

感谢您的帮助。谢谢。

解决方法

您应该更改比例而不是旋转。

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

public class Player : MonoBehaviour
{
    public float speed;
    public float input;
    private Rigidbody2D rb;
    private Animator anim;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if (input != 0)
        {
            print("is running");
            anim.SetBool("isRunning",true);
        }
        else
        {
            print("is not running");
            anim.SetBool("isRunning",false);
        }
        if (input > 0)
        {
            transform.localScale = new Vector3(1,1,1);
        }
        else if (input < 0)
        {
            transform.localScale = new Vector3(-1,1);
        }
    }

    private void FixedUpdate()
    {
        input = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(input * speed,rb.velocity.y);
    }
}