轴方向上的移动
拖拽
1 、获取3D物体在世界坐标的位置转换屏幕坐标
Camera.main.WorldToScreenPoint(dragBeforeGameObjPos)
2、鼠标在屏幕的坐标与物体在屏幕的坐标Z轴进行拟合
Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z))
3、获取3D物体与拟合出来的坐标的偏移量
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
4、通过向量投影得到移动坐标
Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
5、上干货,附带手指控制,手指控制逻辑同鼠标一样
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointAxis : MonoBehaviour
{
public bool IsBeSelected;
[SerializeField]
private PointAxis_Type curPAType;
private bool isMouseDrag = false;
private Vector3 screenPosition;
private Vector3 offset;
private Vector3 dragBeforeGameObjPos;
void Start()
{
//parentTransform = transform.parent;
//mr = GetComponent<MeshRenderer>();
switch (curPAType)
{
case PointAxis_Type.Axis_X:
//colorNormal = ColorNormalX;
break;
case PointAxis_Type.Axis_Y:
//colorNormal = ColorNormalY;
break;
case PointAxis_Type.Axis_Z:
//colorNormal = ColorNormalZ;
break;
}
}
// Update is called once per frame
void Update()
{
#if (UNITY_ANDROID || UNITY_IPHONE)
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
dragBeforeGameObjPos = transform.position;
screenPosition = Camera.main.WorldToScreenPoint(dragBeforeGameObjPos);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z));
isMouseDrag = true;
}
if (Input.touchCount > 0 &&Input.GetTouch(0).phase == TouchPhase.Moved)
{
if (isMouseDrag && IsBeSelected)
{
//Debug.Log("开始拖拽了");
Vector3 currentScreenSpace = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z);
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
// float tempLength = Vector3.Distance(currentPosition, transform.position);
Vector3 tempPos = Vector3.zero;
switch (curPAType)
{
case PointAxis_Type.Axis_X:
tempPos = Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
break;
case PointAxis_Type.Axis_Y:
tempPos = Vector3.Project(currentPosition, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
break;
case PointAxis_Type.Axis_Z:
tempPos = Vector3.Project(currentPosition, transform.forward) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right);
break;
}
transform.position = tempPos;
}
}
if(Input.GetTouch(0).phase == TouchPhase.Ended)
isMouseDrag = false;
#else
if (Input.GetKeyDown(KeyCode.Mouse0))
{
dragBeforeGameObjPos = transform.position;
screenPosition = Camera.main.WorldToScreenPoint(dragBeforeGameObjPos);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
isMouseDrag = true;
}
if (Input.GetKey(KeyCode.Mouse0))
{
if (isMouseDrag && IsBeSelected)
{
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
Vector3 tempPos = Vector3.zero;
switch (curPAType)
{
case PointAxis_Type.Axis_X:
tempPos = Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
break;
case PointAxis_Type.Axis_Y:
tempPos = Vector3.Project(currentPosition, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
break;
case PointAxis_Type.Axis_Z:
tempPos = Vector3.Project(currentPosition, transform.forward) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right);
break;
}
transform.position = tempPos;
}
}
if (Input.GetKeyUp(KeyCode.Mouse0))
isMouseDrag = false;
#endif
}
}
public enum PointAxis_Type
{
Axis_X, Axis_Y, Axis_Z
}
自由拖拽移动
自由拖拽
自由拖拽同上不同的是 获取3D物体与拟合出来的坐标的加上偏移量限制Y轴坐标就搞定了,文章来源:https://www.toymoban.com/news/detail-843531.html
话不多说上干货文章来源地址https://www.toymoban.com/news/detail-843531.html
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PointEntity : MonoBehaviour { public bool IsBeSelected = false; private bool isMouseDrag = false; private Vector3 screenPosition; private Vector3 offset; void Start() { } void Update() { #if (UNITY_ANDROID || UNITY_IPHONE) if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { screenPosition = Camera.main.WorldToScreenPoint(gameObject.transform.position); offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z)); isMouseDrag = true; } if (Input.touchCount > 0 &&Input.GetTouch(0).phase == TouchPhase.Moved) { if (isMouseDrag && IsBeSelected) { //Debug.Log("开始拖拽了"); Vector3 currentScreenSpace = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z); Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset; currentPosition = new Vector3(currentPosition.x, transform.position.y, currentPosition.z); gameObject.transform.position = currentPosition; } } if(Input.GetTouch(0).phase == TouchPhase.Ended) isMouseDrag = false; #else if (Input.GetKeyDown(KeyCode.Mouse0)) { screenPosition = Camera.main.WorldToScreenPoint(gameObject.transform.position); offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z)); isMouseDrag = true; } if (Input.GetKey(KeyCode.Mouse0)) { if (isMouseDrag && IsBeSelected) { //Debug.Log("开始拖拽了"); Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z); Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset; currentPosition = new Vector3(currentPosition.x, transform.position.y, currentPosition.z); gameObject.transform.position = currentPosition; } } if (Input.GetKeyUp(KeyCode.Mouse0)) { isMouseDrag = false; } #endif } }
到了这里,关于Unity中鼠标控制3D物体进行拖拽的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!