【问题标题】:Moving a sprite body causes jitter移动精灵身体会导致抖动
【发布时间】:2015-05-07 18:15:07
【问题描述】:

我对统一开发相当陌生。我想知道为什么当我移动一个精灵对象时,它会导致非常明显的抖动。我已经在互联网上安静地阅读了一些答案,有些人说的是关于刚体 2d 的事情,一些是关于固定或延迟更新等的。但我的问题似乎并没有被任何一个解决。我只有一个精灵对象,没有刚体,只有一个带有精灵渲染器组件的游戏对象和一个附加到它的脚本,位于一个包含相机的空项目中。这是我正在使用的整个脚本,您只需将粘贴复制到名为 MoveBackground 的 cs 文件中即可。

using UnityEngine;
using System.Collections;

public class MoveBackground : MonoBehaviour {

Vector3 pointA;
Vector3 pointB;
float timeRequired;
public float speed = 5;
IEnumerator movingEnum;
float belowEdge;

// Use this for initialization
void Start () {

    Camera cam = Camera.main;
    float height = 2f * cam.orthographicSize;
    belowEdge = cam.transform.position.y - height/2f;
    movingEnum = StartMoving ();
    initializeAndMoveObject ();
}

// Update is called once per frame
void Update () {
    // transform.Translate(0, (-1*speed * Time.deltaTime) / 100, 0);
    //this.gameObject.transform.Translate (0f , Time.deltaTime * speed,0f);
}

void MoveToNextPosition()
{
    float distance = Mathf.Abs( Mathf.Sqrt (Mathf.Pow ((pointA.x - pointB.x), 2f) + Mathf.Pow ((pointA.y - pointB.y), 2f)));
    float time = distance/speed;
    timeRequired = time;
    movingEnum.MoveNext ();
}

IEnumerator StartMoving()
{
    while (true) {
    yield return StartCoroutine(MoveObject(transform, pointA, pointB, timeRequired));
    }
}

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

}

public void initializeAndMoveObject()
{
    Vector3 moveTowardsPoint;
    SpriteRenderer sR = GetComponent<SpriteRenderer> ();
    Vector3 bounds = sR.bounds.extents;
    float height = bounds.y * 2f;
    moveTowardsPoint = transform.position;
    moveTowardsPoint.y = belowEdge - height / 2f;
    pointA = transform.position;
    pointB = moveTowardsPoint;
    MoveToNextPosition ();
}

}

此脚本从上到下移动对象。此外,当我在编辑器中的计算机上运行它时,我没有抖动。但是当我在设备 (iPhone6+) 上运行它时,它会不断地引起非常大的恼人抖动,具体取决于它向下移动的速度。

这不是我尝试使对象移动的唯一方法,我尝试过使用

transform.Translate(0, (-1*speed * Time.deltaTime) / 100, 0);

在更新和固定更新无济于事。为此,我尝试使用 Quad。 (我的游戏对象基本上是一个向下移动的 Bg)。 quad 脚本的一段代码是

public float speed = -0.5f;
private Vector2 savedOffset;

void Start () {
    savedOffset = renderer.sharedMaterial.GetTextureOffset ("_MainTex");
}

void Update () {
    float y = Mathf.Repeat ((Time.time * speed), 1);
    Vector2 offset = new Vector2 (savedOffset.x, y);
    renderer.sharedMaterial.SetTextureOffset ("_MainTex", offset);
}

void OnDisable () {
    renderer.sharedMaterial.SetTextureOffset ("_MainTex", savedOffset);
}

如果有人可以指导我,我会非常感谢你。

【问题讨论】:

  • 第一句标签unity是“不要在关于UNITY游戏引擎的问题上使用这个(改用:unity3d)!!”

标签: c# unity3d


【解决方案1】:

对我来说,这个问题是通过将代码中的 targetFrameRate 设置为 iPhone6+ 的 60 解决的(设置 30 不起作用,设置 -1 不起作用),任何高于 60 的值也可以工作,但低于 60 会导致抖动。所以我所做的只是,一旦我的应用程序启动,我必须执行这个语句(代码中的任何地方都可以工作,但最好是 Awake,在我的场景中它不必处于 Awake 中)。

Application.targetFrameRate = 60;

Unity 文档参考:http://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html

附带说明,并非所有设备都需要 60 帧速率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多