精华内容
下载资源
问答
  • Unity鼠标拖动物体和UI

    2020-06-24 23:04:28
    鼠标拖动物体 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Test : MonoBehaviour{ Vector3 screenSpace; Vector3 offset; ...

    鼠标拖动物体

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    public class Test : MonoBehaviour{
    
        Vector3 screenSpace;
        Vector3 offset;
    	void Start () {
    		
    	}
    	
    	// Update is called once per frame
    	void Update () {
    
    
        }
        private void OnMouseDown()
        {
    
            screenSpace = Camera.main.WorldToScreenPoint(transform.position);
            offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3( Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        }
    
        private void OnMouseDrag()
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
            transform.position = mousePos + offset;
        }
    }
    
    
    

    拖动UI

       public void OnDrag(PointerEventData eventData)
        {
            RectTransform rect = GetComponent<RectTransform>();
            Vector3 pos=Vector3.zero;
            RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, eventData.position, eventData.enterEventCamera,out pos);
            rect.position = pos;
        }
    

    如果此博客对你有用帮忙点个赞,如果有错误请指正。谢谢!

    展开全文
  • using System.Collections; using System.Collections.Generic; using UnityEngine; /// <.../// 鼠标控制自旋 /// </summary> public class SpinWithMouse : MonoBehaviour { pr...
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 鼠标控制自旋
    /// </summary>
    public class SpinWithMouse : MonoBehaviour {
    
        private bool isClick = false;
        private Vector3 nowPos;
        private Vector3 oldPos;
        float length = 1;
    
        void Start()
        {
            AddBoxCollider();
        }
    
        void OnMouseUp() { //鼠标抬起
            isClick = false;
        }
    
        void OnMouseDown() { //鼠标按下
    
            isClick = true;
        }
    
        void Update() {
            nowPos = Input.mousePosition;
            if (isClick) { //鼠标按下不松手
                Vector3 offset = nowPos - oldPos;
                if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y) && Mathf.Abs(offset.x) > length) { //进行旋转
                    transform.Rotate(Vector3.up,-offset.x);
                }
            }
            oldPos = Input.mousePosition;
        }
    
        /// <summary>
        /// 添加Collider作为鼠标检测区域
        /// </summary>
        public void AddBoxCollider()
        {
            BoxCollider box = gameObject.AddComponent<BoxCollider>();
            //参数
            box.center = new Vector3(0, 1.2f, 0);
            box.size = new Vector3(2.4f, 2.4f, 2.4f);
        }
    }

     本来想用射线检测,发现unity生命周期自己带这个,挺方便的,脚本直接挂在物体上就行

    转载于:https://www.cnblogs.com/sanyejun/p/9118674.html

    展开全文
  • public class shejian : MonoBehaviour {  RaycastHit pangzhaungdain;  bool show=false;  // Use this for initialization  void Start () {    }    // Update is called once per... void Update

    public class shejian : MonoBehaviour {

     RaycastHit pangzhaungdain;
     bool show=false;
     // Use this for initialization
     void Start () {
      
     }
     
     // Update is called once per frame
     void Update () {
      if (Input.GetMouseButton (0)) {
          Ray sx = Camera.main.ScreenPointToRay (Input.mousePosition);
          if (Physics.Raycast (sx, out pangzhaungdain) && (null != pangzhaungdain.collider))
            show = true;
          if (show) {
            Vector3 currentposion = new Vector3 (sx.origin.x, sx.origin.y, transform.position.z);
            transform.position = currentposion;

          }
        }else {
            show = false;  
          }
        
     }
    }

    展开全文
  • public Camera camera;  Ray ray;  RaycastHit hitInfo;  Vector3 offset;  GameObject obj;//获取点击到的物体  public GameObject transformObj;  Vector3 startMousePosition;  // Use this
        public Camera camera;
        Ray ray;
        RaycastHit hitInfo;
        Vector3 offset;
        GameObject obj;//获取点击到的物体
        public GameObject transformObj;
        Vector3 startMousePosition;
        // Use this for initialization
        void Start () {
            startMousePosition = Input.mousePosition;
        }
    
    // Update is called once per frame
        void Update () {
            if (Input.GetMouseButtonDown (0)) {
                ray = camera.ScreenPointToRay (Input.mousePosition);
                if (Physics.Raycast (ray, out hitInfo)) {
                     obj = GameObject.Find (hitInfo.transform.name);
                    obj.GetComponent<MeshRenderer>().material.color = new Color(Random.Range (0f, 1f),Random.Range (0f, 1f),Random.Range (0f, 1f),Random.Range (0f, 1f));
                }
            }
            if (Input.GetMouseButton (0)) {
                if (obj) {
                    //将物体由世界坐标转为屏幕坐标ScreenToWorldPoint
                    Vector3 screenObj = Camera.main.WorldToScreenPoint(obj.transform.position);
                    //保存鼠标的屏幕坐标 z使用目标物体的屏幕坐标
                    Vector3 screenMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y,screenObj.z);
                    offset =Camera.main.ScreenToWorldPoint(screenMouse)- obj.transform.position ;
                    Vector3 currentPosition = obj.transform.position + offset;
                    obj.transform.position = currentPosition;
                }
            }
        
            if (Input.mousePosition != startMousePosition && !Input.GetMouseButton(0)&& !Input.GetMouseButtonDown(0)) {
                Vector3 screenObj = Camera.main.WorldToScreenPoint(transformObj.transform.position);
                Vector3 screenMouse = Input.mousePosition;
                Vector3 newVector = screenMouse - screenObj;
                transformObj.transform.forward = newVector;
            }
        }
    
    
    
    
    	transform.position = new Vector3 (Mathf.MoveTowards (transform.position.x, ball.transform.position.x, speed * Time.deltaTime), Mathf.MoveTowards (transform.position.y, ball.transform.position.y- 2f, speed * Time.deltaTime), Mathf.MoveTowards (transform.position.z, ball.transform.position.z , speed * Time.deltaTime));
    
    坐标转换参考文档点击打开链接
    展开全文
  • unity鼠标拖动物体,鼠标缩放视野,控制旋转物体
  • unity 鼠标拖动三维物体移动

    千次阅读 2018-03-03 18:19:36
    下面是讲的是鼠标拖动3D游戏物体 using UnityEngine; using System.Collections; using System.Collections.Generic; public class MouseMove : MonoBehaviour {  //鼠标经过时改变物体颜色  private Co...
  • #pragma strict var numberAverages : int = 3; private var originalRotation : Quaternion; private var ...将上面脚本挂在需要拖动物体上 FR:徐海涛(hunk Xu) QQ技术群:386476712    
  • Unity 拖动鼠标物体旋转

    千次阅读 2016-08-07 10:45:32
    Unity 拖动鼠标物体旋转查看通过拖动鼠标左键来旋转物体,基本原理是将鼠标拖动的距离转换为物体旋转的角度。假设Cubeusing UnityEngine; using System.Collections;public class NewBehaviourScript : ...
  • Unity2D拖动物体笔记

    2020-05-25 16:06:06
    Unity2D拖动物体笔记 unity拖动物体的方式有很多。 最简单就是检测鼠标的经过,按下和抬起。当鼠标按下时,使得物体跟随着鼠标,也就是两者的位置信息保持一致。 需要注意的是,物体的坐标属于世界坐标,鼠标的坐标...
  • unity通过鼠标拖动物体移动

    千次阅读 2019-01-24 16:49:13
     //获取到鼠标的位置(鼠标水平的输入和竖直的输入以及距离)  Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);  //物体的位置,屏幕坐标转换为世界坐标  ...
  • 鼠标拖动三维物体移动  private Vector3 _vec3TargetScreenSpace;// 目标物体的屏幕空间坐标  private Vector3 _vec3TargetWorldSpace;// 目标物体的世界空间坐标  private Transform _trans;// 目标...
  • 鼠标拖动物移动在游戏设计上用的非常多,像飞机射击...首先将鼠标的屏幕坐标转换为三维的世界坐标,这样为了计算鼠标物体位置之间的距离,再将移动后的鼠标的屏幕坐标转换为世界坐标,加上之前的距离量,将和值...
  • Unity鼠标控制物体 拖动旋转

    千次阅读 2018-07-24 12:31:41
    public class Script2 : MonoBehaviour {  Ray ray;  RaycastHit hit;  GameObject Obj;... Vector3 _鼠标世界坐标_;  Vector3 _Obj坐标差值_;  float _水平旋转_;  float _垂直旋转_;  ...
  • Unity 3D - 鼠标拖动物体Demo :using System.Collections; using System.Collections.Generic; using UnityEngine;public class Drag : MonoBehaviour { private Vector3 _vec3TargetScreenSpace;// 目标物体的屏幕...
  • 这篇博客实现一个小功能,通过鼠标拖动3D物体。我们知道,如果是拖动 UIUG 的控件的话,它是有接口可以使用的。但是3D物体就没有可直接使用的接口(或者说我没找到?),就需要我们自己写代码。既然如此,那我们就来...
  • using UnityEngine; using System.Collections; public class mouse_Drag : MonoBehaviour { public Camera mCamera; public float deth = 10f;... //鼠标放到物体上 void OnMouseEnter() { thi
  • 当我们做一些农场游戏或者类似可编辑战场类游戏时经常会遇到从UI上拖动物体在3D场景里生成,要实现这种从UI上拖物体的效果其实挺简单,代码不复杂,主要是想法很重要,一个优秀的程序员不只是技术扎实,更重要的是要...
  • Unity3d中使用鼠标拖动物体移动

    千次阅读 2013-11-10 15:13:37
    //unity3d用鼠标拖动物体的一段代码 var mouseOverColor = Color.blue;//声明变量为蓝色 private var originalColor:Color;//声明变量来存储本来颜色 function Start() { originalColor = renderer....
  • 这是一段拖动物体的代码,比较简洁明了,对了解unity3d脚本概念有些帮助!在此加上注释分享! 02 var mouseOverColor = Color.blue;//声明变量为蓝色 03 private var originalColor : Color;//声明变量存储本来...
  • 1.鼠标拖动物体Cube using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; /// /// 脚本挂到一个Cube上 /// public class Ts : MonoBehaviour...
  • 这篇博客实现一个小功能,通过鼠标拖动3D物体。我们知道,如果是拖动 UIUG 的控件的话,它是有接口可以使用的。但是3D物体就没有可直接使用的接口(或者说我没找到?),就需要我们自己写代码。既然如此,那我们就来...

空空如也

空空如也

1 2 3 4 5 ... 10
收藏数 192
精华内容 76
关键字:

unity鼠标拖动物体