zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }

    使用方法

    image image

    Drag预览

    drag

  • 相关阅读:
    git常用操作的总结
    Vue全局错误捕获
    Vue全局布局组件的设计
    Python 创建守护进程
    常用代码集合
    Linux -- 终端命令快捷键汇总
    MySQL -- 查询结果 直接导出至本地文件
    Linux -- 守护进程
    ConcurrentWebSocketSessionDecorator(并发安全的websocket) The remote endpoint was in state [TEXT_PARTIAL_WRITING]
    Ubuntu安装docker
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/3619645.html
Copyright © 2011-2022 走看看