【unity实用技能】在GameObject前画一条线

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chrisfxs/article/details/73800859

在项目中有时候需要在scene里划线方面检查

这里面有两点,划线和人物正前方的计算

划线用LineRanderer

人物正前方是

go.transform.forward + go.transform.position

人物的坐标加上人物的正前方方向

(我一开始死脑筋的时候想着正前方就go.transform.forward 不就是人物的正前方吗,但是如果不加上自己的坐标,它只是一个方向,也就是这个坐标只是相对于原点的方向,加上坐标才是你要的点)


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawLine : MonoBehaviour {
    public LineRenderer line;
    public GameObject go;
	// Use this for initialization
	void Start () {

	}

    void Update()
    {
        Vector3 targetPos = go.transform.forward + go.transform.position;
        line.SetPosition(0, go.transform.position);
        line.SetPosition(1, targetPos);
    }
}


猜你喜欢

转载自blog.csdn.net/chrisfxs/article/details/73800859