[英]Unity sprite flip goes out of camera bounds
我试图在按下箭头键时翻转精灵,但它们以某种方式超出了相机的范围。 在我看来,整个相机被翻转而不是精灵。 解决这个问题的正确方法是什么。
当用户按左/右箭头键时,我试图翻转。 下面是我的代码。
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);
}
}
任何帮助表示赞赏。 谢谢。
您应该更改比例而不是旋转。
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.