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的更多相关文章
随机推荐
- Eclipse下Android开发的问题:Failed to install AndroidPhone.apk on device 'emulator-5554': timeout 解决办法
在window->preferences->Android->DDMS->ADB connection time out (ms): 将这个值设置的大一些,默认为5000,我设 ...
- 设计模式之Builder (创建者模式)的一些个人理解(转)
对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程的Director要.刚才google到一篇文章,总算清楚了.在这里转贴一下 ...
- LALR(1)语法分析生成器--xbytes
0.概述: 看了编译器龙书和虎书后,自己手动写了一个LALR(1)语法分析生成器,使用的语法文件格式和lemon的差不多. 程序里面很多的算法也都是摘录自虎书,龙书虽然讲的很详细,但是真正动手写的时候 ...
- .net学习总结
.NET 学前入门 了解.Net能做什么 了解.NET,C#语言及其特点(分清.NET和C#的关系),对.Net学习有系统全面的认识. C#基础 变量,赋值运算符.数据类型转换等. 选择结构控制(if ...
- JavaScript强化教程——Cocos2d-JS中JavaScript继承
javaScript语言本身没有提供类,没有其它语言的类继承机制,它的继承是通过对象的原型实现的,但这不能满足Cocos2d-JS引擎的要求.由于Cocos2d-JS引擎是从Cocos2d-x演变而来 ...
- IOS开发中NSRunloop跟NSTimer的问题
在Windows时代,大家肯定对SendMessage,PostMessage,GetMessage有所了解,这些都是windows中的消息处理函数,那对应在ios中是什么呢,其实就是NSRunloo ...
- iOS之通过PaintCode快速实现交互动画的最方便方法 未解问题
需求: 问题: 源码百度云下载链接: http://pan.baidu.com/s/1o7r4hCm 密码: 8atd 其他学习链接:http://www.jianshu.com/p/90d6cd35 ...
- Burp Suite安装及详细使用教程-Intruder模块详解
01 介绍 安装要求: Java 的V1.5 + 安装( 推荐使用最新的JRE ), 可从这里免费 http://java.sun.com/j2se/downloads.html Burp Suite ...
- Effective Java 66 Synchronize access to shared mutable data
synchronized - Only a single thread can execute a method or block at one time. Not only does synchro ...
- JavaScript Patterns 3.5 JSON
JSON: JavaScript Object Notation {"name": "value", "some": [1, 2, 3]} ...