我想在没有 StartCoroutine 的情况下使用 Vector3.Lerp() 移动精灵。要在脚本中设置起点和目标点。我将精灵拖放到 Unity 编辑器中并运行它。然而,精灵并没有移动。谢谢。
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class MyScript1 : MonoBehaviour {
public Sprite sprite;
GameObject gameObject;
SpriteRenderer spriteRenderer;
Vector3 startPosition;
Vector3 targetPosition;
void Awake()
{
gameObject = new GameObject();
spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
}
private void Start()
{
spriteRenderer.sprite = sprite;
startPosition = new Vector3(-300, 100, 0);
targetPosition = new Vector3(100, 100, 0);
}
void Update()
{
transform.position = Vector3.Lerp(startPosition, targetPosition , Time.deltaTime*2f);
}
}