Unity LineRenderer繪製滑鼠方向線段
using System.Collections;using System.Collections.Generic;using UnityEngine;public class NewBehaviourScript : MonoBehaviour { public static Vector3 startPointOfLine; public static Vector3 endPointOfLine; public static bool flag = false; //public Material mat; private Transform _t; private Rigidbody2D _rigidbody2D; private LineRenderer _drawLine; void Start () { _t = this.gameObject.transform; _drawLine = this.GetComponent<LineRenderer> ();// _drawLine.SetWidth (1f,1f);// _drawLine.SetColors(Color.white,Color.yellow); _rigidbody2D = this.GetComponent<Rigidbody2D> (); } // Update is called once per frame void Update () { if (Input.GetMouseButton(0)) { _drawLine.enabled = true; startPointOfLine = _t.position; endPointOfLine = Camera.main.ScreenToWorldPoint ( new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1.0f)); if (startPointOfLine != endPointOfLine) { this.drawLineObj (); } } //收起按鈕 if (Input.GetMouseButtonUp (0)) { _drawLine.enabled = false; Vector3 mw = Camera.main.ScreenToWorldPoint (Input.mousePosition); Vector3 tw = _t.position; Vector3 velocity = mw - tw; _rigidbody2D.velocity = new Vector2 (velocity.x, velocity.y) * 2f; } } public void drawLineObj(){ _drawLine.useWorldSpace = true; _drawLine.SetPosition (0, startPointOfLine); _drawLine.SetPosition (1, endPointOfLine); }}
推薦閱讀:
※Unity中的單例模式、回調函數、消息分發的使用區別?
※教你做Unity第一個遊戲Roll A Ball(上)
※Moba手游優化總結
※使用頂點投射的方法製作實時陰影
※Unity接入多個SDK的通用介面開發與資源管理(三)
TAG:unity |