工具 unity2018 vs2017
资源:要移动的模型 ,一个三维坐标轴
结果(暂时没找到办法放本地录屏,就先放图了):
通过鼠标拖动xyz三个轴,实现物体在三维空间的移动(可以是本地坐标也可以是世界坐标,看个人需求,demo里我是让物体在世界坐标系移动)
一、制作一个三维坐标轴。如图
xyz三个轴分别创建一个空的子节点,放置到末端,(是为了获得三个轴的末端映射到屏幕上的坐标)如图:
二、再创建一个摄影机。只负责渲染坐标轴这个层,添加层Axis,同时指定坐标轴为该层
主摄影机不渲染该层,但是要保证两个摄影机的位置和旋转要保持一致,坐标轴摄影机按照以上修改,保证该摄影机后渲染
三、写脚本
CameraController:负责两个摄影机的拉进拉远以及旋转,为了测试在不同视角下,我们移动物体的方向都没有错
MoveTargetItem:负责通过拖动坐标轴来实现移动目标物体
脚本内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform cameraParent;
private const float MOVE_SPEED = 0.1F;
private const float ROTATE_SPEED = 0.1F;
private Vector3 lastPos = Vector3.zero;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
lastPos = Input.mousePosition;
}
if (Input.GetMouseButton(1))//鼠标右键按下 旋转摄影机
{
RotateCamera();
}
MoveCamera();
}
private void MoveCamera()
{
float mouse= Input.GetAxis("Mouse ScrollWheel");
Vector3 pos = cameraParent.Find("Camera").position;
if (mouse < 0)
{
pos.z -= MOVE_SPEED;
}
if (mouse > 0)
{
pos.z += MOVE_SPEED;
}
cameraParent.Find("Main Camera").position = pos;
cameraParent.Find("Camera").position = pos;
}
private void RotateCamera()
{
float dx = Input.mousePosition.x - lastPos.x;
float dy = Input.mousePosition.y - lastPos.y;
Vector3 rotation = cameraParent.rotation.eulerAngles;
rotation.x -= dy*ROTATE_SPEED;
rotation.y += dx* ROTATE_SPEED;
cameraParent.rotation = Quaternion.Euler(rotation);
lastPos = Input.mousePosition;
}
}
//鼠标滚轮实现拉进拉远
//鼠标右键实现旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveTargetItem : MonoBehaviour
{
public Transform axis; //坐标轴模型
public Transform cube; //要移动的物体
public Transform axisCamera; //只渲染坐标轴的摄像机
private const float MOVE_SPEED = 0.005F;
private int currentAxis = 0;//1:x 2:y 3:z 要移动的轴
private bool choosedAxis = false;
private Vector3 lastPos; //上一帧鼠标位置
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = axisCamera.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000, 1 << LayerMask.NameToLayer("Axis"))) //只检测Axis这一层
{
choosedAxis = true;
lastPos = Input.mousePosition;
if (hit.collider.name == "x") { currentAxis = 1; }
if (hit.collider.name == "y") { currentAxis = 2; }
if (hit.collider.name == "z") { currentAxis = 3; }
}
}
if (Input.GetMouseButton(0) && choosedAxis)
{
UpdateCubePosition();
}
if (Input.GetMouseButtonUp(0))
{
choosedAxis = false;
currentAxis = 0;
}
}
private void UpdateCubePosition()
{
Camera camera = axisCamera.GetComponent<Camera>();
Vector3 origin = camera.WorldToScreenPoint(axis.position); //三个坐标轴向量的原点对应屏幕坐标
Vector3 mouse = Input.mousePosition - lastPos; //鼠标两帧之间的移动轨迹在屏幕上的向量
Vector3 axisEnd_x = camera.WorldToScreenPoint(axis.Find("x/x").position); //三个坐标轴的终点对应屏幕坐标
Vector3 axisEnd_y= camera.WorldToScreenPoint(axis.Find("y/y").position);
Vector3 axisEnd_z = camera.WorldToScreenPoint(axis.Find("z/z").position);
Vector3 vector_x = axisEnd_x - origin; //x轴对应屏幕向量
Vector3 vector_y = axisEnd_y - origin;
Vector3 vector_z = axisEnd_z - origin;
Vector3 cubePos = cube.position;
float d = Vector3.Distance(Input.mousePosition, lastPos) * MOVE_SPEED; //鼠标移动距离
if (currentAxis == 1)
{
//鼠标移动轨迹与X轴夹角的余弦值
float cos= Mathf.Cos(Mathf.PI / 180 * Vector3.Angle(mouse, vector_x));
if (cos < 0) { d = -d; }
cubePos.x += d;
cube.position = cubePos;
axis.position = cubePos;
}
if (currentAxis == 2)
{
float cos = Mathf.Cos(Mathf.PI / 180 * Vector3.Angle(mouse, vector_y));
if (cos < 0) { d = -d; }
cubePos.y += d;
cube.position = cubePos;
axis.position = cubePos;
}
if (currentAxis == 3)
{
float cos = Mathf.Cos(Mathf.PI / 180 * Vector3.Angle(mouse, vector_z));
if (cos < 0) { d = -d; }
cubePos.z += d;
cube.position = cubePos;
axis.position = cubePos;
}
lastPos = Input.mousePosition;
}
}
大致思路:通过鼠标选中要移动的轴,获得该轴的终点对应的屏幕坐标,以及坐标轴原点对应的屏幕坐标,得到该轴在屏幕上的向量1;通过鼠标的移动,获得鼠标两帧之前移动位置构成的向量2;求得两个向量夹角的余弦值;判断该余弦值的正负,如果移动的是x轴,余弦值正,就将x轴加上该值否则减去该值(也可以是余弦值*移动的距离)
其实还有很多可以完善的地方,比如,选中某个轴就让某个轴高亮显示,这样效果更明显,更换一下坐标轴的材质就可以了。