为什么当角色走路时,它没有画出红线?

时间:2017-02-18 17:07:54

标签: c# unity3d unity5

一般来说,我想在一个行走的角色后面实时绘制一条红线。该线应该从第一个原始角色位置开始,然后在玩家走路时绘制,以标记玩家到目前为止走的路。

现在我还希望每次角色达到一个方向点时画一条红线。红色线将在原始角色位置和第一个到达路径点之间绘制,然后下一次到达第二个路径点以从第一个路径点到第二个路径点绘制一条线等等所有路径点和最后一条线在最后一个方向点之间的原始角色位置。

但由于某种原因,它根本没有画线。我希望在游戏运行时在游戏窗口中绘制线条。

现在我正在使用Debug.DrawLine,但它没有绘制任何东西。也许我不是在调试窗口的正确位置?我在游戏窗口下看到了控制台窗口。无论如何,我想在游戏运行时在游戏窗口中绘制线条。

using UnityEngine;
using System.Collections;

public class MoveObject : MonoBehaviour {

    public Transform target;
    float moveSpeed = 3f;
    float rotationSpeed = 3f;
    Transform myTransform;
    State state;
    public enum State
    {
        Idle,
        Way1,
        Way2,
        Way3,
        Way4
    }
    void Awake()
    {
        myTransform = transform;
    }
    // Use this for initialization
    void Start()
    {
        Debug.Log("Scripts Strart");
        state = State.Idle;
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Update");
        switch (state)
        {
            case State.Idle:
                Idle();
                break;
            case State.Way1:
                waypoint1();
                break;
            case State.Way2:
                waypoint2();
                break;
            case State.Way3:
                waypoint3();
                break;
            case State.Way4:
                waypoint4();
                break;
        }
    }
    public void Idle()
    {
        state = State.Way1;
    }
    void waypoint1()
    {
        target = GameObject.Find("W1").transform;
        float distance = Vector3.Distance(myTransform.position, target.transform.position);
        Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        if (distance < 2f)
            state = State.Way2;
    }
    void waypoint2()
    {
        target = GameObject.Find("W2").transform;
        float distance = Vector3.Distance(myTransform.position, target.transform.position);
        Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        if (distance < 2f)
            state = State.Way3;
    }
    void waypoint3()
    {
        target = GameObject.Find("W3").transform;
        float distance = Vector3.Distance(myTransform.position, target.transform.position);
        Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        if (distance < 2f)
            state = State.Way4;
    }
    void waypoint4()
    {
        target = GameObject.Find("W4").transform;
        float distance = Vector3.Distance(myTransform.position, target.transform.position);
        Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        if (distance < 2f)
            state = State.Way1;
    }
}

2 个答案:

答案 0 :(得分:0)

Debug.DrawLine仅显示在“场景视图”中。如果您希望自己的线条显示游戏名称,则需要使用LineRenderer

答案 1 :(得分:0)

让你在移动时尝试LineRenderer添加一个点....

https://docs.unity3d.com/Manual/class-LineRenderer.html