鼠标拖动出现直线碰撞器
2015-08-21 18:07
246 查看
using UnityEngine; using System.Collections; public class DrawPhysicsLine : MonoBehaviour { private LineRenderer line; // Reference to LineRenderer private Vector3 mousePos; private Vector3 startPos; // Start position of line private Vector3 endPos; // End position of line void Update() { // On mouse down new line will be created if (Input.GetMouseButtonDown(0)) { if (line == null) createLine(); mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0; line.SetPosition(0, mousePos); startPos = mousePos; } else if (Input.GetMouseButtonUp(0)) { if (line) { mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0; line.SetPosition(1, mousePos); endPos = mousePos; addColliderToLine(); line = null; } } else if (Input.GetMouseButton(0)) { if (line) { mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0; line.SetPosition(1, mousePos); } } } // Following method creates line runtime using Line Renderer component private void createLine() { line = new GameObject("Line").AddComponent<LineRenderer>(); line.material = new Material(Shader.Find("Diffuse")); line.SetVertexCount(2); line.SetWidth(0.1f, 0.1f); line.SetColors(Color.black, Color.black); line.useWorldSpace = true; } // Following method adds collider to created line private void addColliderToLine() { BoxCollider col = new GameObject("Collider").AddComponent<BoxCollider>(); col.transform.parent = line.transform; // Collider is added as child object of line float lineLength = Vector3.Distance(startPos, endPos); // length of line col.size = new Vector3(lineLength, 0.1f, 1f); // size of collider is set where X is length of line, Y is width of line, Z will be set as per requirement Vector3 midPoint = (startPos + endPos) / 2; col.transform.position = midPoint; // setting position of collider object // Following lines calculate the angle between startPos and endPos float angle = (Mathf.Abs(startPos.y - endPos.y) / Mathf.Abs(startPos.x - endPos.x)); if ((startPos.y < endPos.y && startPos.x > endPos.x) || (endPos.y < startPos.y && endPos.x > startPos.x)) { angle *= -1; } angle = Mathf.Rad2Deg * Mathf.Atan(angle); col.transform.Rotate(0, 0, angle); } }
相关文章推荐
- Centos中将openjdk切换为Oracle Jdk导致Tomcat无法正常启动的问题
- IOS XIB Cell自适应高度实现
- 读取shp属性表导出为excel表格和写入word文档
- android学习笔记之客户端与服务端保持session登录状态
- Windows文件换行符转Linux换行符
- axis2 1.5.5 + Websphere 8.0 ,FileUploadException 与 NoInitialContextException 的解决办法
- 现今市场对异构并行计算领域人员的需求
- Unity3d之流光效果
- APEC计划指引我们前进:云计算服务将上升
- 黑马程序员--设计模式
- CSS中行高line-height属性的一些使用技巧
- UFLDL教程(三)---Softmax回归
- 内部排序
- 【iOS程序启动与运转】- RunLoop个人小结
- Dll方式的线程,需要引用这个
- 子域名爆破&C段查询&调用Bing查询同IP网站
- xcode6+ios8最新真机调试教程
- CC2530作为协调器或路由器,最大可以带多少子节点
- PHP路由技术的原理与实践
- ionic 中的navigation,tabbar及返回按钮定制