【问题标题】:What is the best way to move game objects, smoothly rather than teleporting it?平滑移动游戏对象而不是传送它的最佳方法是什么?
【发布时间】:2019-07-01 13:45:21
【问题描述】:

我正在尝试将游戏对象 pokemon 从屏幕中心平滑地移动到屏幕左侧(仅沿水平轴)。但是,它会立即传送。

代码的作用 = 在无限循环中从一个 sprite 更改为另一个 sprite。当它变成另一个精灵时,它也改变了它的比例。

我想要代码做什么 = 在更改为不同的精灵之前,在水平轴上向左移动。

我尝试过线性插值 (lerp) 和 transform.translate。

public class Transitions : MonoBehaviour
{

    public Sprite Pokemon_0; // VARIABLES FOR SPRITES (Pokemons/ game objects) 
    public Sprite Pokemon_1;

    float timer = 1f; // CHANGING SPRITES VALUES (delays)
    float delay = 5f;

    Vector2 temp;     //CHANGING POSITION VARIABLES
    Vector2 tempPos;
    Vector2 finalPosition;


    void Start()
    {
        this.gameObject.GetComponent<SpriteRenderer>().sprite = Pokemon_0;
    }

    void Update()
    {
        timer -= Time.deltaTime;
    if (timer <= 0)
    {
        if (this.gameObject.GetComponent<SpriteRenderer>().sprite == Pokemon_0) // TO CHANGE FROM POKEMON 1 TO POKEMON 2 
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = Pokemon_1;

            temp = transform.localScale;  // TO SET THE SCALE WHEN EACH POKEMON CHANGES (works)
            temp.x = -380;
            temp.y = 350;
            transform.localScale = temp;
            timer = delay;

            finalPosition = new Vector2(167, -107);
            transform.position = Vector3.Lerp(transform.position, finalPosition, 1f * Time.deltaTime);

            //tempPos = transform.position; // Tried and tested method. Doesn't work. 
            //tempPos.x = 1f;
            //transform.position = tempPos;



            return;

        }
        if (this.gameObject.GetComponent<SpriteRenderer>().sprite == Pokemon_1) // TO CHANGE BACK FROM POKEMON 2 TO POKEMON 1
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = Pokemon_0;
            timer = delay;
            return;

        }

    }

在执行时,此代码确实会改变口袋妖怪并改变比例。但是,位置保持不变。

我是新开的,所以我一直在尝试我们的不同建议,谢谢。

【问题讨论】:

  • 您绝对应该为此使用 Lerp:docs.unity3d.com/ScriptReference/Vector3.Lerp.html
  • 能否请您发布一个最小的可重现示例?我猜在这种情况下,它至少会是整个类的代码。
  • 给它一个刚体和力?添加 1 个单位也可能有点像它每帧添加 1 个。如果你得到 200fps,它会飞过去
  • 该代码在Update() 方法中吗?
  • @DmitriyPopov 嗨,刚刚发布了整个课程并解释了它的作用。我愿意接受建议。

标签: c# unity3d


【解决方案1】:

最好使用补间引擎,例如http://dotween.demigiant.com/

如果你安装了 Dotween,那么你可以简单地使用

transform.DOMove(new vector3(1 ,0 , 1) , duration);

您还可以为补间设置轻松。或使用 Oncomplete 函数;

transform.DOMove(new vector3(1 ,0 , 1) , duration)
    .SetEase(Ease.OutCubic)
    .OnCompelete(() => { shouldClose = true; }); 

您还可以为补间.SetLoops(-1) 设置循环。

【讨论】:

    【解决方案2】:

    在您的代码示例中,您似乎正在以不同的方式使用位置的 x 值。 x y 和 z 值一起使用以形成一个点,该点在 3D 环境中表示。将对象的位置设置为之前的位置,而每次将 x 值更改为 1 都会将其锁定在水平轴上。

    您可以使用插值来平滑两个位置状态之间的过渡,也可以使用 Unity 的内置 Vector3.MoveTowards(或 Vector2,如果在 2D 风景中)。

    插值

    您想要的位置是您想要更改的确切位置。您当前的职位将是transform.position

    float step; // Set this to how fast you want to increment movement (1f, 5f, 10f)
    Vector3 start; // Set this to the transform.position when you want to start interpolating
    Vector3 desired; // Set this to a new Vector3(x, y) with the place you want to move to
    
    void Update(){
        // Here, we set the object's position to it's current position, but interpolated to it's desired position
        transform.position = Vector3.Lerp(start, desired, step * Time.deltaTime);
    }
    

    前进

    float step; // Set this to how fast you want to increment movement (1f, 5f, 10f)
    Vector3 desired; // Set this to a new Vector3(x, y) with the place you want to move to
    
    void Update(){
        transform.position = Vector3.MoveTowards(transform.position, desired, step * Time.deltaTime);
    }
    

    【讨论】:

    • 您好,两种方法都试过了。不幸的是,它们对我不起作用。似乎他们什么都不做。我的精灵停留在同一个地方。我刚刚用我的整个班级更新了这篇文章并解释了代码的作用,任何帮助将不胜感激。
    • Lerp 采用起点和终点,而不是当前和终点。它还应该有办法在某个时候达到 1。 MoveTowards 设置为执行极小的动作。
    • @Everts 谢谢,我已经更新了答案。只要精灵位于带有脚本的 GameObject 上,它就应该可以工作。如果这似乎没有任何作用,请尝试将 step 变量更改为更大的变量
    • 在这种情况下,lerp 的问题在于,对象一开始会快速移动,然后向目标减速,随着它越来越近,它的移动速度会非常慢。这是因为每次移动都是剩余距离的一小部分。所以它作为一个限制。需要的是增加比率,使其达到 1 并完成运动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多