我需要让精灵水平移动预定距离,水平翻转,然后返回



我正在尝试让企鹅精灵沿着我制作的平台来回滑动。我已经做到了这一点,但是一旦精灵向后移动,我真的很难让它水平翻转。我相信我可能需要改变我让企鹅移动的方法,以便结合翻转。我看到很多关于围绕 Y 轴旋转的线程,但我不知道如何合并它。就目前而言,企鹅总是朝向正x方向来回走动。该游戏在 Unity 上采用 2D 形式,并用 C# 编写。感谢您的任何建议。:)

using UnityEngine;
using System.Collections;
public class Enemy1 : MonoBehaviour {
private SpriteRenderer SpriteRenderer;
public float min = 2f;
public float max = 3f;
public int x = 0;
public bool facingRight = true;
 // Use this for initialization
void Start()
{
    min = transform.position.x;
    max = transform.position.x + 27;
}
// Update is called once per frame
void Update()
{
    transform.position = new Vector3(Mathf.PingPong(Time.time * 2, max - min) + min, transform.position.y, transform.position.z);
}

}

你可以跟踪你的精灵的方向并使用localScale.xflipX

float _prevX = 0f;
...
void Update()
{
    float newX = Mathf.PingPong (Time.time * 2, max - min);
    transform.position = new Vector3(newX + min, transform.position.y, transform.position.z);
    Vector3 scale = transform.localScale;
    scale.x = newX < _prevX ? -1 : 1;
    transform.localScale = scale;
    _prevX = newX;
}

另一种方式:跟踪你的精灵何时靠近"边缘"点,然后才"翻转",否则,如果你由于某些原因提前旋转你的精灵,第一种方法会更通用。

自从我弄乱精灵以来已经有一段时间了,但我认为设置 transform.scale.z = -1 会翻转精灵,使其面向另一个方向。

最新更新