establish DragCube Script mounted in Cube upper
The code is as follows
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DragCube: MonoBehaviour { private Camera cam;// A camera that emits rays private
GameObject go;// Objects collided by rays public static string btnName;// The name of the object that the ray collides with private Vector3
screenSpace; private Vector3 offset; private bool isDrage = false; Vector3
scale; void Start() { cam = Camera.main; scale = gameObject.transform.localScale
; } void Update() { // Overall initial position Ray ray = cam.ScreenPointToRay(Input.mousePosition)
; // The ray from the camera to the click coordinates RaycastHit hitInfo; if (isDrage == false) { if (Physics.
Raycast(ray, out hitInfo)) { // Draw out the rays , Only in scene View Debug.DrawLine(ray.origin,
hitInfo.point); go = hitInfo.collider.gameObject; //print(btnName); screenSpace
= cam.WorldToScreenPoint(go.transform.position); offset = go.transform.position
- cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.
y, screenSpace.z)); // The name of the object btnName = go.name; // The name of the component } else { btnName = null;
} } if (Input.GetMouseButton(0)) { Vector3 currentScreenSpace = new Vector3(
Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); Vector3
currentPosition= cam.ScreenToWorldPoint(currentScreenSpace) + offset; if (
btnName!= null) { go.transform.position = currentPosition; } isDrage = true; }
else { isDrage = false; } if (Input.GetAxis("Mouse ScrollWheel") < 0) { //if
(Camera.main.fieldOfView <= 100) // Camera.main.fieldOfView += 2; if (scale.x<5)
{ scale.x += 0.2f; scale.y += 0.2f; scale.z += 0.2f; gameObject.transform.
localScale= scale; } } //Zoom in if (Input.GetAxis("Mouse ScrollWheel") > 0) {
// Zoom camera angle fieldOfView value //if (Camera.main.fieldOfView > 40) //
Camera.main.fieldOfView -= 2; // Zoom objects if (scale.x > 0) { scale.x -= 0.2f; scale.y
-= 0.2f; scale.z -= 0.2f; gameObject.transform.localScale = scale; } } } }

Technology
©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )