码迷,mamicode.com
首页 > 移动开发 > 详细

unity3d中点击一个按钮实现相机之间的转换,点选已添加物体,并使用键盘实现移动、鼠标实现旋转、缩放操作

时间:2017-10-29 11:06:53      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:input   ==   sep   engine   cal   system   void   ica   unit   

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

public class build : MonoBehaviour {
  public GameObject camera_main;
  public GameObject camera_front;
  public int cameraflag;


  public Vector3 pos;
  
  public GameObject Abuild;
  public Dropdown drp;
  public float x, y;
  public float speed= 10.0f;
  void Start()
  {
    camera_main = GameObject.Find("Camera");
    camera_front = GameObject.Find("Camera_front");
    camera_front.SetActive(false);
    cameraflag = 1;
  }

  void Update () {

    //按住鼠标左键旋转模型
    if (Input.GetMouseButton(0)&&Abuild!=null)
    {

      //鼠标按着左键移动
      y = Input.GetAxis("Mouse X") * Time.deltaTime * speed;
      x = Input.GetAxis("Mouse Y") * Time.deltaTime * speed;
      Abuild.transform.Rotate(new Vector3(x, y, 0));

    }

    //键盘上下左右键实现模型的移动
    if (Input.GetAxis("Horizontal") != 0 && Abuild != null)
    {
      Abuild.transform.Translate(Input.GetAxis("Horizontal") * 3, 0, 0);
    }
    if (Input.GetAxis("Vertical") != 0 && Abuild != null)
    {
      Abuild.transform.Translate(0, 0,Input.GetAxis("Vertical") * 3);
    }

    //鼠标滑轮实现模型的放缩
    if (Input.GetAxis("Mouse ScrollWheel") != 0)
    {
      Abuild.transform.localScale += new Vector3(Input.GetAxis("Mouse ScrollWheel"), Input.GetAxis("Mouse ScrollWheel"), Input.GetAxis("Mouse ScrollWheel"));
    }

    if (Input.GetMouseButtonDown(1) && cameraflag > 0)
    {
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      RaycastHit hit;

    if (Physics.Raycast(ray, out hit))
    {
      pos = hit.point;
      if (hit.transform.gameObject.name == "Plane")
        pick = GameObject.Instantiate(Resources.Load("选中"), pos, transform.rotation) as GameObject;
      else
      {
        Debug.Log("已选中物体!");
        Abuild = hit.transform.gameObject;
      }
    }
  }
  //实现相机之间转换的函数
  public void changecamera()
  {
    cameraflag = -cameraflag;
    if (cameraflag > 0)
    {
      camera_main.SetActive(true);
      camera_front.SetActive(false);
    }
    else
    {
      camera_main.SetActive(false);
      camera_front.SetActive(true);
      Abuild = null;
    }
  }
}

unity3d中点击一个按钮实现相机之间的转换,点选已添加物体,并使用键盘实现移动、鼠标实现旋转、缩放操作

标签:input   ==   sep   engine   cal   system   void   ica   unit   

原文地址:http://www.cnblogs.com/xuyt/p/7749376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!