【问题标题】:Why when the character is walking it's not drawing a red line?为什么角色走路时没有画红线?
【发布时间】:2017-02-21 17:58:56
【问题描述】:

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

现在我还想在角色每次到达一个路点时画一条红线。红线将在原始字符位置和第一个到达的路点之间绘制,然后下次到达第二个路点,从第一个路点到第二个路点画一条线,依此类推所有路点和最后一行最后一路指向原字符位置之间。

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

现在我正在使用 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;
    }
}

【问题讨论】:

    标签: c# unity3d unity5


    【解决方案1】:

    Debug.DrawLine 仅显示在场景视图中。如果你想让你的台词出现在游戏中,你需要使用LineRenderer

    【讨论】:

    • 是的,Debug 类中的任何内容都可能不打算从最终用户的角度来看。
    【解决方案2】:

    让我们试试 LineRenderer 在你移动的时候加一个点....

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

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2021-05-09
      相关资源
      最近更新 更多