使用Unity 3D的C#:当用户移动鼠标时,如何让相机在对象周围移动

时间:2015-12-06 12:48:12

标签: c# object unity3d camera mouse

我正在尝试在Unity 4中进行3D观看模拟,用户可以在其中选择一个对象并移动鼠标围绕它旋转(360度)我已经拍了很多镜头试图让它工作,但我每个都失败了时间,任何帮助将不胜感激,如果它是用C#写的那就太棒了! (但它没有) 提前谢谢!

4 个答案:

答案 0 :(得分:3)

MouseOrbit脚本执行此操作:

http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23

只需将此脚本附加到相机对象中,然后在检查器中链接目标对象。

答案 1 :(得分:0)

这很完美。我做的唯一改变是向Camera Orbit添加一个脚本:

import tensorflow as tf
import data


def main(argv):
    (train_x, train_y), (test_x, test_y) = data.load_data()

    alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
                "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0"]

    categorical_column_1 = tf.feature_column.categorical_column_with_vocabulary_list(key='Char1',
                                                                                     vocabulary_list=alphabet,
                                                                                     default_value=0, dtype=tf.string)
    ...
    categorical_column_16 = tf.feature_column.categorical_column_with_vocabulary_list(key='Char16',
                                                                                      vocabulary_list=alphabet,
                                                                                      default_value=0, dtype=tf.string)

    my_feature_columns = [
        tf.feature_column.indicator_column(categorical_column_1),
        ...
        tf.feature_column.indicator_column(categorical_column_16),

    ]

    classifier = tf.estimator.DNNClassifier(
        feature_columns=my_feature_columns, hidden_units=[16, 16], n_classes=4
    )

    batch_size = 100
    train_steps = 100

    classifier.train(
        input_fn=lambda: data.train_input_fn(train_x, train_y, batch_size),
        steps=train_steps)

    eval_result = classifier.evaluate(
        input_fn=lambda: data.test_input_fn(test_x, test_y, batch_size)
    )

    print("\nAccuracy with test data: {accuracy:0.2f}\n".format(**eval_result))


if __name__ == '__main__':
    tf.logging.set_verbosity(tf.logging.INFO)
    tf.app.run(main)

然后附上你的"播放器"对象输入控件,输入控件将转到播放器所在的位置,允许您跟踪播放器,以及旋转和鼠标滚轮缩放。花式。

localScale if语句意味着您目前只能放大和缩小。

此脚本现在唯一的问题是,如果缩小到15然后继续尝试缩小,相机会反弹。我确信这很容易解决,但我还没有把时间花在上面。

答案 2 :(得分:0)

您根本不需要CameraController,只需将相机的z旋转设置为-90。

答案 3 :(得分:0)

-用于鼠标按下并拖动 -我在这里修改了代码:http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23

public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;

public float yMinLimit = -20f;
public float yMaxLimit = 80f;

public float distanceMin = .5f;
public float distanceMax = 15f;

private Rigidbody rigidbody;

float x = 0.0f;
float y = 0.0f;

float mouseX = 0f;
float mouseY = 0f;

// Use this for initialization
void Start()
{
    Vector3 angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    rigidbody = GetComponent<Rigidbody>();

    // Make the rigid body not change rotation
    if (rigidbody != null)
    {
        rigidbody.freezeRotation = true;
    }
}

void LateUpdate()
{
    if (target)
    {
        GetMouseButtonDown_XY();

        x += mouseX * xSpeed * distance * 0.02f;
        y -= mouseY * ySpeed * 0.02f;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        Quaternion rotation = Quaternion.Euler(y, x, 0);

        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

        RaycastHit hit;
        if (Physics.Linecast(target.position, transform.position, out hit))
        {
            distance -= hit.distance;
        }
        Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
        Vector3 position = rotation * negDistance + target.position;

        transform.rotation = rotation;
        transform.position = position;
    }
}

public static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360F)
        angle += 360F;
    if (angle > 360F)
        angle -= 360F;
    return Mathf.Clamp(angle, min, max);
}

Vector3 mousePosPrev;
void GetMouseButtonDown_XY()
{
    if (Input.GetMouseButtonDown(0))
    {
        mousePosPrev = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    } 

    if (Input.GetMouseButton(0))
    {
        Vector3 newMousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);

        if (newMousePos.x < mousePosPrev.x)
        {
            mouseX = -1;
        } else if (newMousePos.x > mousePosPrev.x)
        {
            mouseX = 1;
        } else
        {
            mouseX = -0;
        }

        if (newMousePos.y < mousePosPrev.y)
        {
            mouseY = -1;
        }
        else if (newMousePos.y > mousePosPrev.y)
        {
            mouseY = 1;
        }
        else
        {
            mouseY = -0;
        }

        mousePosPrev = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    }
}