组件源码

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的更多相关文章

随机推荐

  1. C++之内联函数与constexpr

    inline 函数 规模小,流程直接且频繁调用 cout<<shortString(s1,s2)<<endl; = cout<<(s1.size()<s2.s ...

  2. [Architecture Pattern] Singleton Locator

    [Architecture Pattern] Singleton Locator 目的 组件自己提供Service Locator模式,用来降低组件的耦合度. 情景 在开发系统时,底层的Infrast ...

  3. 【JavaEE】SSH+Spring Security自定义Security的部分处理策略

    本文建立在 SSH与Spring Security整合 一文的基础上,从这篇文章的example上做修改,或者从 配置了AOP 的example上做修改皆可.这里主要补充我在实际使用Spring Se ...

  4. CSS属性选择器温故-4

    1.属性选择器就是通过元素属性来找到元素 2.属性选择器语法 CSS3遵循了惯用的编码规则,通配符的使用提高了样式表的书写效率,也使CSS3的属性选择器更符合编码习惯 3.浏览器兼容性 CSS选择器总 ...

  5. 简洁侧边wordpress博客模板

    模板描述:商务领航,尽现成熟稳重的个人小站风格     响应式Web设计,自适应电脑.平板电脑.移动设备     图标字体库,自适应各种终端设备,保证图形图标清晰无锯齿,支持Retina(视网膜屏幕) ...

  6. jekyll

    bundle show minima查看安装路径 bundle exec github-pages versions 建立一个类似于master的分支,与master是完全独立 git checkou ...

  7. SAP中的Currency Converting Factor

    ABAP编程中,有个概念很重要,即Currency Converting Factor(货币转换因子).可能很多ABAP初学者都不知道这是什么东西,这里我们就简单探讨下. 1. 什么是货币转换因子 在 ...

  8. 用QQ号登陆Sharepoint,研究到最后关头卡住了。大家发力呀

    此项目未完成,登陆不了SharePoint,大家研究吧,折腾吧..... 已经完成的部分有:已经可以获取到腾讯用户信息,如: Get Access Token===============access ...

  9. Effective Java 28 Use bounded wildcards to increase API flexibility

    Get and Put Principle PECS stands for producer-extends(? extends T), consumer-super(? super T). For ...

  10. MongoDB学习笔记——聚合操作之group,distinct,count

    单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...