unity3d中點擊一個按鈕實現相機之間的轉換,點選已添加物體,並使用鍵盤實現移動、滑鼠實現旋轉、縮放操作

來源:互聯網
上載者:User

標籤: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中點擊一個按鈕實現相機之間的轉換,點選已添加物體,並使用鍵盤實現移動、滑鼠實現旋轉、縮放操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.