DragRigidbody2D
组件源码
using UnityEngine;
using System.Collections; //This script allows to drag rigidbody2D elements on the scene with orthographic camera
//Attach this script to your camera public class DragRigidbody2D : MonoBehaviour
{
public float Damper = 5f;
public float Frequency = 3;
public float Drag = 10f;
public float AngularDrag = 5f; private SpringJoint2D _springJoint; private Camera _camera;
private RaycastHit2D _rayHit; void Start ()
{
_camera = gameObject.GetComponent<Camera>();
} void Update ()
{
if (!Input.GetMouseButtonDown(0))
return; //Looking for any collider2D under mouse position
_rayHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (_rayHit.collider == null)
return; if (!_rayHit.collider.rigidbody2D || _rayHit.collider.rigidbody2D.isKinematic)
return; if (!_springJoint)
{
//Create spring joint
GameObject go = new GameObject("[Rigidbody2D_dragger]");
Rigidbody2D body = go.AddComponent<Rigidbody2D>();
_springJoint = go.AddComponent<SpringJoint2D>();
body.isKinematic = true;
} _springJoint.transform.position = _rayHit.point; _springJoint.anchor = Vector2.zero; //Apply parameters to spring joint
_springJoint.frequency = Frequency;
_springJoint.dampingRatio = Damper;
_springJoint.distance = 0;
_springJoint.connectedBody = _rayHit.collider.rigidbody2D; StartCoroutine("DragObject");
} IEnumerator DragObject()
{
var oldDrag = _springJoint.connectedBody.drag;
var oldAngDrag = _springJoint.connectedBody.angularDrag; _springJoint.connectedBody.drag = Drag;
_springJoint.connectedBody.angularDrag = AngularDrag; while (Input.GetMouseButton(0))
{
Vector2 newPos = _camera.ScreenToWorldPoint(Input.mousePosition);
_springJoint.transform.position = new Vector2(newPos.x, newPos.y);
yield return new WaitForSeconds(0.1f);
} if (_springJoint.connectedBody)
{
_springJoint.connectedBody.drag = oldDrag;
_springJoint.connectedBody.angularDrag = oldAngDrag;
_springJoint.connectedBody = null;
}
}
}
使用方法
Drag预览
DragRigidbody2D的更多相关文章
随机推荐
- DirectShow程序运行过程简析
这段时间一直在学习陆其明老师的<DirectShow开发指南>一书,书中对DirectShow的很多细节讲解清晰,但是却容易让人缺少对全局的把握.在学习过程中,整理了关于DirectSho ...
- andriod ==和equals
== 用于数字 equals用于字符
- Create a “% Complete” Progress Bar with JS Link in SharePoint 2013
Create a “% Complete” Progress Bar with JS Link in SharePoint 2013 SharePoint 2013 has a lot new fea ...
- 新著作计划:《水利水电工程施工导流 水力计算与.NET编程》
目 录 第一篇 基础理论篇 第1章 施工导截流设计概述 第2章 基本水力计算 2.1 临界水深计算 2.2 正常水深计算 2.3 堰流水力计算 2.4 明渠流水力计算 2.5 管流水力计算 第3章 ...
- ubuntu下nagios配置
参考文献: http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html http://my.oschina.net/duangr/blo ...
- Jsoup解析Html中文文档
jsoup 简介Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HTM ...
- java多线程系列5-死锁与线程间通信
这篇文章介绍java死锁机制和线程间通信 死锁 死锁:两个或两个以上的线程在争夺资源的过程中,发生的一种相互等待的现象. 同步代码块的嵌套案例 public class MyLock { // 创建两 ...
- Asp.net MVC 4新项目中创建area的后续操作
Asp.net MVC 4新项目中创建area后,往往HomeController与area的HomeController路由发生混淆,需要手工设置一些地方避免mvc无法识别默认路由的状况. 无废话具 ...
- Effective Java 54 Use native methods judiciously
Java Native Interface(JNI) allows Java applications to call native methods, which are special method ...
- Apache CXF自定义拦截器
为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器 ...