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的更多相关文章
随机推荐
- JavaScript 中有关数组对象的方法
JS 处理数组多种方法 js 中的数据类型分为两大类:原始类型和对象类型. 原始类型包括:数值.字符串.布尔值.null.undefined 对象类型包括:对象即是属性的集合,当然这里又两个特殊的对象 ...
- [模仿][JS]新浪财经7*24直播
使用新浪财经7*24直播的数据 简单的做一个山寨品 在线地址:[痛苦啊,有GFW,却没有vpn,往heroku上传浪费了好多时间...] http://wangxinsheng.herokuapp.c ...
- 初识python(1)
1.python简介 Python是一种面向对象.直译式计算机程序语言.也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定. Python语法简捷而清晰,具有丰富和强大的类库.它 ...
- SharePoint 使用PowerShell恢复误删的网站集
在SharePoint网站集的使用中,我们很有可能会误删我们需要的网站集,SharePoint其实并没有把网站集删掉,只是放到了SPDeletedSite中,这样,我们还可以通过PowerShell找 ...
- Android 去掉标题和状态栏 达到全屏显示
1,正常显示的标题和状态栏的截图. 2,具体代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCrea ...
- C# 零散知识 扩展方法 类型约束
今天看到这么一段代码,我看下面调用了NotifyPropertyChanged定义了两个参数,但是调用的时候只写了一个参数.后来查了下,原来这个是扩展方法的用法, 就是说给T扩展了一个方法Notify ...
- OC的项目网址(自己编写的项目)
因为便于方便快速打开自己曾经写过的项目,所以就把链接保存在博客里了.一点击就能找到. <附注学习github排版:https://github.com/yangxuanxc/guide?file ...
- 使用 eclipse+egit 将项目提交至 github ,本地的git仓库:eclipse工作项目目录
新建github仓库 写一个github上仓库的名字,系统会自动检测重复性,无重复则可以提交 大于号代表有需要提交的东西 ...
- OBIEE 11g 启动与停止包含服务器重启
ORACLE_BIEE_HOME为biee安装路径 注意:默认建立的是"instance1"但是如果你安装过多次可能实例名是不一样(例如: instance2以此类推).因此,请找 ...
- 页面间(窗口间)的取值赋值及获取iframe下的window对象
①同一个窗口中,获取某个iframe的信息 <body> <iframe id="PAID" name="PA" src="Item ...