翻转图像/动画,使其遵循移动方向
本文关键字:移动 方向 图像 动画 翻转 | 更新日期: 2023-09-27 18:29:01
大家好,感谢您阅读本文。
我在游戏中为我的"怪物"制作了一个小动作脚本。
using UnityEngine;
using System.Collections;
public class MonsterMovement : MonoBehaviour {
public Vector3 pointB;
bool facingLeft = true;
IEnumerator Start()
{
var pointA = transform.position;
while (true)
{
yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
}
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i = 0.0f;
var rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
这个脚本很简单,它将对象/怪物从a->b移动回来。并且不断重复。
但我怎样才能翻转对象的图像,使其跟随运动的方向呢。?
我真的希望你能帮助我。
非常感谢。
我找到了解决问题的方法。
我添加了这个小代码:
bool facingLeft = true;
void Flip(){
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
然后更改while函数:
while (true)
{
yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
if (!facingLeft) {
Flip();
}
yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
if (facingLeft) {
Flip();
}
}