Sprite在告诉它移动后不显示

问题描述 投票:0回答:2

在我最近的代码更新中,在将预制部件拉入层次结构之前,该精灵不再想要显示,它将显示并且仍会执行该操作。但是,每当我使用代码生成它时,图像都不会显示。我所做的更新会生成精灵,然后将它们向下移动。我可以在检查器中看到精灵的移动,但在屏幕上看不到它。

MoveEnemy脚本

public class MoveEnemy : MonoBehaviour
{

    [HideInInspector]
    public GameObject[] waypoints;
    private int currentWaypoint = 0;
    private float lastWaypointSwitchTime;
    public float speed = 1.0f;

    // Start is called before the first frame update
    void Start()
    {
        lastWaypointSwitchTime = Time.time;
    }

    // Update is called once per frame
    void Update()
    {
        // 1
        Vector3 startPosition = waypoints
        [currentWaypoint].transform.position;
        Vector3 endPosition = waypoints[currentWaypoint + 1].transform.position;

        // 2
        float pathLength = Vector3.Distance(startPosition, endPosition);
        float totalTimeForPath = pathLength / speed;
        float currentTimeOnPath = Time.time - lastWaypointSwitchTime;
        gameObject.transform.position = Vector2.Lerp(startPosition, endPosition, currentTimeOnPath / totalTimeForPath);

        // 3
        if (gameObject.transform.position.Equals(endPosition))
        {
            if (currentWaypoint < waypoints.Length - 2)
            {
                // 3.a
                currentWaypoint++;
                lastWaypointSwitchTime = Time.time;
                // TODO: Rotate into move direction
            }
            else
            {
                // 3.b
                Destroy(gameObject);

                AudioSource audioSource = gameObject.GetComponent<AudioSource>();
                AudioSource.PlayClipAtPoint(audioSource.clip, transform.position);
                // TODO: deduct health
            }
        }
    }
}

// Credit raywenderlich.com

SpawnEnemy脚本

public class SpawnEnemy : MonoBehaviour
{

    public GameObject[] waypoints;
    public GameObject testEnemyPrefab;

    // Start is called before the first frame update
    void Start()
    {
        Instantiate(testEnemyPrefab).GetComponent<MoveEnemy>().waypoints = waypoints;
    }
c# unity3d
2个回答
0
投票

我认为物体在相机后面产生。向后移动相机一点。将视图设置为3d世界空间。然后沿z轴移动相机。或更改衍生点的z轴。


0
投票

我很笨。原因是因为我忘记了当我将画布渲染模式更改为“屏幕空间-相机”时,我为运动设置的航点不会随画布一起移动,所以当我将画布设置为X = 1000,Y = 1000 ish时看着50、50 ish。

© www.soinside.com 2019 - 2024. All rights reserved.