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的更多相关文章
随机推荐
- ES6的Class
类的基本写法: constructor构造函数其实就相当于ES5中的构造函数,用于定义类的实例属性: 而在类中定义的其他方法像这里的toString方法就相当于ES5中定义在原型prototype上的 ...
- 使用RDCMan管理SharePoint虚拟机的重复要求验证的问题
首先,这个软件可以从这里下载: Remote Desktop Connection Manager 同类型的软件还有很多,我没有很多复杂功能的要求,就选择了这款微软官方的,虽然很久都没有更新过了. 为 ...
- Level 4 A10: 飞张?
看来庄家的红桃2个输张没法解决,只能寄希望于飞K了. 但如果将牌2-2分布,还有更稳的打法.在下面这种东家3张黑桃的情况时,庄家只需垫到红桃2就行了. 如果东家有4张黑桃,那就只有飞红桃K这一条路了.
- Android系统提供的开发常用的包名及作用
android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容提供者浏览和 ...
- 搭建Android 5.0开发环境
1.Android SDK的安装 下载地址:http://developer.android.com/index.html 访问网站的话请自备梯子 选择:adt-bundle-windows-x86_ ...
- 异步get请求之Block方法
#import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
前言:使用NSURLSessionDownloadTask满足不这个需要离线断点续传的下载需求,所以这里就需要使用NSURLSessionDataTask的代理方法来处理下载大文件,并且实现离线断点续 ...
- Android 之 自动匹配字符AutoCompleteTextView
AutoCompleteTextView是自动匹配字符,当我们输入一个单词或一段话的前几个字时,就会自动为你匹配后面的内容看效果图: 下面是代码: MainActivit: package com.e ...
- 生命游戏/Game of Life的Java实现(转)
首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...
- 软件测试作业3--Junit、hamcrest、eclemmat的安装和使用
1. how to install junit, hamcrest and eclemma? 首先下载下来Junit和Hamcrest的jar包,然后新建项目的时候将这两个jar包导入到工程里面就 ...