框内对象之间的绑定拖动

时间:2018-12-29 08:53:10

标签: c# unity3d graphics monodevelop bounds

Room containing sphere and cube

嗨,

作为我项目的一部分,我在盒子里有一个球体和一个立方体。用户通过使用鼠标拖动多维数据集。球体移动到框内的随机坐标。长期以来,我一直在努力寻找如何将两个对象绑定到盒子的方法,盒子由6个直角棱镜组成(每个墙体+屋顶+地板一个)。

请大家帮我弄清楚该怎么做吗?

用于控制球体和立方体的代码发布在下面。

领域

using System.Collections;
using System.IO;

using UnityEngine;

public class PlayerController : MonoBehaviour
{
private float movementDuration = 2.0f;
private WaitForSeconds waitBeforeMoving = new WaitForSeconds(2f);
private Vector3[] path = new Vector3[20];
private string filepath = null;

private void Start()
{
    StartCoroutine(MainRoutine());
    filepath = Application.dataPath + "/Player.txt";
    File.WriteAllText(filepath, "The player blob visited these random coordinates: \n\n");
}

private IEnumerator MainRoutine()
{
    //generate new path:
    for (int i = 0; i < path.Length; i++)
    {
        float timer = 0.0f;
        Vector3 startPos = transform.position;
        float x = RandomNum(timer);
        float y = RandomNum(x);
        float z = RandomNum(y);
        path[i] = new Vector3(x, y, z);
    }

    //traverse path:
    for (int i = 0; i < path.Length; i++)
    {
        float timer = 0.0f;
        Vector3 startPos = transform.position;
        while (timer < movementDuration)
        {
            timer += Time.deltaTime;
            float t = timer / movementDuration;
            //t = t * t * t * (t * (6f * t - 15f) + 10f);
            transform.position = Vector3.Lerp(startPos, path[i], t);
            yield return null;
        }
        yield return waitBeforeMoving;
    }

    //print path:
    PrintPoints();
}

private void PrintPoints()
{

    foreach (Vector3 vector in path)
    {
        File.AppendAllText(filepath, string.Format("{0}\n\n", JsonUtility.ToJson(vector)));
    }
}

private float RandomNum(float lastRandNum)
{
    //Random value range can be changed in the future if necessary
    float randNum = Random.Range(-30.0f, 30.0f);
    return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
}

}

多维数据集

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

[RequireComponent(typeof(MeshCollider))]


public class UserController : MonoBehaviour
{
private Vector3 Dist;
private float PosX = 0.0f;
private float PosY = 0.0f;
private float PosZ = 0.0f;
private bool shiftOn = false;

private void OnMouseDown()
{

        Dist = Camera.main.WorldToScreenPoint(transform.position);
        PosX = Input.mousePosition.x - Dist.x;
        PosY = Input.mousePosition.y - Dist.y;
        PosZ = Input.mousePosition.z - Dist.z;

}
private void OnMouseDrag()
{

    if (Input.GetMouseButton(0))
    {
        if (shiftOn)
        {
            //3D Drag, courtesy of Unity Forums
            //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
            transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
        }

        else
        {
            //Plane Drag
            //float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
            Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - PosX, Input.mousePosition.y - PosY, Input.mousePosition.z - PosZ));
            transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
        }



    }

}



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

    if (Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift))
    {
        Debug.Log("Shift Pressed"); //Logs message to the Unity Console.
        shiftOn = true;
    }

    if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
    {
        Debug.Log("Shift Released"); //Logs message to the Unity Console.
        shiftOn = false;
    }



}



}

0 个答案:

没有答案