当前位置:网站首页>随便画画的
随便画画的
2022-06-25 22:42:00 【KillJUMP】




using UnityEngine;
public class Grabber : MonoBehaviour {
private GameObject selectedObject;
private void Update() {
if (Input.GetMouseButtonDown(0)) {
if(selectedObject == null) {
RaycastHit hit = CastRay();
if(hit.collider != null) {
if (!hit.collider.CompareTag("drag")) {
return;
}
selectedObject = hit.collider.gameObject;
Cursor.visible = false;
}
} else {
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
selectedObject.transform.position = new Vector3(worldPosition.x, 0f, worldPosition.z);
selectedObject = null;
Cursor.visible = true;
}
}
if(selectedObject != null) {
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
selectedObject.transform.position = new Vector3(worldPosition.x, .25f, worldPosition.z);
if (Input.GetMouseButtonDown(1)) {
selectedObject.transform.rotation = Quaternion.Euler(new Vector3(
selectedObject.transform.rotation.eulerAngles.x,
selectedObject.transform.rotation.eulerAngles.y + 90f,
selectedObject.transform.rotation.eulerAngles.z));
}
}
}
private RaycastHit CastRay() {
Debug.Log(Input.mousePosition.x);
Camera camera_ = GetComponent<Camera>();
Debug.Log(GetComponent<Camera>().farClipPlane);
Vector3 screenMousePosFar = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
GetComponent<Camera>().farClipPlane);
Vector3 screenMousePosNear = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
GetComponent<Camera>().nearClipPlane);
Vector3 worldMousePosFar = camera_.ScreenToWorldPoint(screenMousePosFar);
Vector3 worldMousePosNear = camera_.ScreenToWorldPoint(screenMousePosNear);
RaycastHit hit;
Physics.Raycast(worldMousePosNear, worldMousePosFar - worldMousePosNear, out hit);
Debug.Log(hit.collider);
return hit;
}
}
边栏推荐
- 使用VS2022编译Telegram桌面端(tdesktop)
- 鼠标拖拽围绕某个物体旋转展示
- mysql
- Daily question brushing record (IV)
- How to bypass SSL authentication
- Correct writing methods of case, number and punctuation in Chinese and English papers
- CaMKIIa和GCaMP6f是一樣的嘛?
- Explanation of chip processing manufacturer__ What is ICT? What is the main test? Advantages and disadvantages of ICT testing?
- Function and principle of SPI solder paste inspection machine
- Solution to SMT grape ball phenomenon
猜你喜欢
随机推荐
Idea kotlin version upgrade
Methods to realize asynchrony
Compile the telegraph desktop side (tdesktop) using vs2022
How to design the product roadmap?
Is camkiia the same as gcamp6f?
Explain from a process perspective what happens to the browser after entering a URL?
Binary sort tree
Flink报错:Error: A JNI error has occurred, please check your installation and try again
Core ideas of SQL optimization
CaMKIIa和GCaMP6f是一样的嘛?
【OEM专场活动】清“芯”夏日,有奖征文
Installation and configuration of gradle environment
After being trapped by the sequelae of the new crown for 15 months, Stanford Xueba was forced to miss the graduation ceremony. Now he still needs to stay in bed for 16 hours every day: I should have e
渗透工具-Burpsuite
删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?
Drag the mouse to rotate the display around an object
Web學習之TypeScript
Research and development practice of Kwai real-time data warehouse support system
1-11solutions to common problems of VMware virtual machine
Explanation of chip processing manufacturer__ What is ICT? What is the main test? Advantages and disadvantages of ICT testing?









