简体   繁体   English

使用鼠标在3D空间中向各个方向移动对象

[英]Moving an object in all directions utilizing a mouse in 3D-space

I have this tracking project that I have worked on for a long time. 我已经从事了很长时间的跟踪项目。 Basically, the User moves a cube to trace the movements of a sphere which moves randomly. 基本上,用户移动一个立方体以跟踪随机移动的球体的运动。 Currently, I am working with the User movements. 目前,我正在处理用户运动。

The issue is that the device that the user utilizes to move in all directions is a mouse in combination with the arrow keys. 问题在于,用户用来在所有方向上移动的设备是结合了箭头键的鼠标。 This is because the mouse handles two dimensions (xy), while the arrow keys handle two dimensions (xz). 这是因为鼠标处理二维(xy),而箭头键处理二维(xz)。 However, I would like to make it possible for me to just use the mouse. 但是,我想让我可以只使用鼠标。

As such, my professor suggested that I use the shift key to switch between xy and xz movements. 因此,我的教授建议我使用Shift键在xy和xz运动之间切换。 However, I am confused as to how to go about this. 但是,我对如何解决这个问题感到困惑。

The code below represents what I have at this moment and what I have tried in regards to the shift key movements. 下面的代码表示我目前所拥有的以及关于Shift键运动的尝试。

Could someone please help me solve this issue, or is there a better way to go about this? 有人可以帮我解决这个问题,还是有更好的方法解决这个问题?

Thank you! 谢谢!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshCollider))]

public class UserController : MonoBehaviour {

public int speed = 20;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

    // get input data from keyboard or controller
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    // update player position based on input
    Vector3 position = transform.position;
    position.x += moveHorizontal * speed * Time.deltaTime;
    position.z += moveVertical * speed * Time.deltaTime;
    transform.position = position;
}

void OnMouseDrag()
{

    while(Input.GetMouseButton(0))
    {
        int shiftCalled = 0;
        //3D Drag, courtesy of Unity Forums
        if(Input.GetKey(KeyCode.LeftShift))
        {
            float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
            transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
        }

        //Plane Drag, courtesy of Unity Forums
        if(Input.GetKey(KeyCode.RightShift))
        {
            float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
            Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
            transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
        }
    }





}
}

Here's what I would do - 这就是我要做的-

Imagine a ray going from the position of the mouse on your screen towards a 2D plane that stretches on the X and Z angles. 想象一下,光线从鼠标在屏幕上的位置射向2D平面,该平面在X和Z角上伸展。 The intersection of said ray with the plane would be the X,Z coordinated that you wish your object to be dragged on. 所述射线与平面的交点将是您希望在其上拖动对象的X,Z坐标。 Now imagine the same ray intersecting with a different plane that stretches on the X and Y coordinates. 现在,想象同一条光线与在X和Y坐标上延伸的不同平面相交。 This will generate the other type of movement you are looking for. 这将生成您正在寻找的其他运动类型。

By holding the SHIFT button you will essentially be swapping these two planes, which should generate the result you are looking for. 通过按住SHIFT按钮,您实际上将交换这两个平面,这将生成您想要的结果。

You can generate both planes in code using - 您可以使用-在代码中生成两个平面

xy_Plane = new Plane(Vector3.forward, m_DistanceFromCamera);
xz_Plane = new Plane(Vector3.up, m_DistanceFromCamera);

Generate the rays using - 使用-产生射线

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        //Initialise the enter variable
        float enter = 0.0f;

        if (xy_Plane.Raycast(ray, out enter))
            Vector3 hitPoint = ray.GetPoint(enter);

and check which plane should be used with - 并检查应与哪个飞机一起使用-

if(Input.GetKey(KeyCode.Shift))

Now you only need to move your object based on the hitPoint you received. 现在,您只需要根据收到的命中点移动对象。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM