简体   繁体   English

Unity 精灵翻转超出相机范围

[英]Unity sprite flip goes out of camera bounds

I'm trying to flip a sprite when arrow keys pressed but they somehow goes out of the boudnds of the camera.我试图在按下箭头键时翻转精灵,但它们以某种方式超出了相机的范围。 It seems to me the whole camera is flipped instead of the sprite.在我看来,整个相机被翻转而不是精灵。 What is the right way to approach this.解决这个问题的正确方法是什么。

I'm trying to flip when user press the left / right arrow key.当用户按左/右箭头键时,我试图翻转。 Below is my code.下面是我的代码。

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,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);
    }
}

this is the screenshot from the scene / game这是场景/游戏的截图在此处输入图片说明

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

You should change the scale instead of rotation.您应该更改比例而不是旋转。

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, 1);
        }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM