unity--3D物体的上下左右移动,鼠标控制大小,旋转
来源:互联网 发布:网络攻防基础题库 编辑:程序博客网 时间:2024/05/16 13:58
这次文章主要是看了网上写的很多关于物体上下左右位移,鼠标控制物体的大小,旋转问题,但是下载很多,依然没有自己想要的结果,就自己根据前辈经验,在上面稍加修改,简直就是班门弄斧了。方便自己,也方便大家.这次主要有几点需要说明,就是里面的GameObject需要我们自己去拖,毕竟unity大家为了方便使用,所有能设置为变量的都需要设置变量,一目了然。有点unity基础的都能够看的懂。我上面加的有一个iTween,我作为跳转场景使用了我直接注释掉,也不删除了,看看也可以。
using UnityEngine;using System.Collections;using System.IO;using UnityEngine.SceneManagement;using UnityEngine.UI;public class Move : MonoBehaviour{ public Transform target; private int MouseWheelSensitivity = 50; private int MouseZoomMin = 20; private int MouseZoomMax = 112; private float normalDistance = 500; private Vector3 normalized; private float xSpeed = 250.0f; private float ySpeed = 120.0f; private float x = 0.0f; private float y = 0.0f; private System.DateTime oldTime; private System.DateTime nowTime; //public GameObject plane; //private Vector3 moveTarget; //private bool ismoving = false; //public GameObject cameratarget; //private string nextScene; void Start() { oldTime = System.DateTime.Now; ////transform.LookAt(target); //iTween.MoveBy(plane, iTween.Hash("z", 10, "easeType", iTween.EaseType.easeInCubic, "loopType", iTween.LoopType.pingPong, "delay", 0.1)); } void Update() { //if (Input.GetMouseButtonDown(0)) //{ // Ray ray; // ray = Camera.main.ScreenPointToRay(Input.mousePosition); //定义一条射线,这条射线从摄像机屏幕射向鼠标所在位置 // //首先判断是否点击了鼠标左键 // RaycastHit hit; //声明一个碰撞的点(暂且理解为碰撞的交点) // if (Physics.Raycast(ray, out hit)) //如果真的发生了碰撞,ray这条射线在hit点与别的物体碰撞了 // { // if (hit.transform.CompareTag("NextScene")) // { // string scene = hit.transform.name; // nextScene = scene; // Move(ray.direction); // // SceneManager.LoadScene(scene); // } // } //} // if (Mathf.Abs(cameratarget.transform.localPosition.x - moveTarget.x) > -600 && //Mathf.Abs(cameratarget.transform.localPosition.y - moveTarget.y) < 100 && //Mathf.Abs(cameratarget.transform.localPosition.z - moveTarget.z) > -100) // { // ismoving = true; // SceneManager.LoadScene(nextScene); // } //string name = PlayerPrefs.GetString("filename"); // if (name != "") // { // // target = GameObject.Find(name).transform; //} nowTime = System.DateTime.Now; System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks); System.TimeSpan ts2 = new System.TimeSpan(nowTime.Ticks); System.TimeSpan ts = ts2.Subtract(ts1).Duration(); if (Input.GetMouseButton(1)) { if (Input.GetAxis("Mouse X") < 0) transform.RotateAround(target.position, Vector3.down, 4); if (Input.GetAxis("Mouse X") > 0) transform.RotateAround(target.position, Vector3.up, 4); } else if (Input.GetAxis("Mouse ScrollWheel") != 0) { if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax) { normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity; } if (normalDistance < MouseZoomMin) { normalDistance = MouseZoomMin; } if (normalDistance > MouseZoomMax) { normalDistance = MouseZoomMax; } transform.GetComponent<Camera>().fieldOfView = normalDistance; } else if (Input.GetMouseButton(0)) { if (Input.GetAxis("Mouse Y") < 0) //down { Vector3 temp = Vector3.up * 60.0f * Time.deltaTime; print("wyz==up===" + transform.localPosition.y); if (transform.localPosition.y > 300) { temp = Vector3.up * 5.0f * Time.deltaTime; transform.Translate(temp); } else { transform.Translate(temp); } } if (Input.GetAxis("Mouse Y") > 0) //up { print("wyz==donw===" + transform.localPosition.y); Vector3 temp = Vector3.down * 60.0f * Time.deltaTime; if (transform.localPosition.y < -300) { temp = Vector3.up * 5.0f * Time.deltaTime; transform.Translate(temp); } else { transform.Translate(temp); } } if (Input.GetAxis("Mouse X") < 0) { Vector3 temp = Vector3.right * 100.0f * Time.deltaTime; temp = Vector3.right * 60.0f * Time.deltaTime; transform.Translate(temp); transform.Translate(temp); } if (Input.GetAxis("Mouse X") > 0) { Vector3 temp = Vector3.left * 100.0f * Time.deltaTime; temp = Vector3.left * 60.0f * Time.deltaTime; transform.Translate(temp); transform.Translate(temp); } } } //public void Move(Vector3 dir) //{ // ismoving = true; // moveTarget = dir * 20; // iTween.MoveTo(cameratarget, iTween.Hash("position", moveTarget, "oncomplete", "myClbk", // "loopType", iTween.LoopType.loop, "speed", 15.0f)); //}}
阅读全文
1 0
- unity--3D物体的上下左右移动,鼠标控制大小,旋转
- 【Unity 3D学习】获取鼠标滑动方向(上下左右)-控制摄像机围绕物体旋转
- Unity 3D 控制物体上下左右均匀移动脚本
- unity 3d 鼠标旋转物体脚本
- Unity鼠标控制物体360°旋转和点击移动
- unity 根据鼠标的移动来旋转物体Rotate
- Unity 3D鼠标操作实现旋转、缩放观察物体
- unity中根据鼠标移动旋转物体
- unity 手指移动物体旋转两个手指控制物体大小缩放
- 鼠标控制摄像机旋转,平移,物体的移动
- unity 鼠标控制摄像机围绕物体旋转
- unity中控制物体的移动,和旋转
- unity 3d中简单巧妙的实现拖动鼠标旋转物体
- unity 控制物体移动和旋转
- Unity 控制物体移动 旋转 缩放
- Unity用鼠标控制物体移动
- Unity 3D 控制物体前后左右均匀移动脚本
- Unity鼠标自由查看3D物体之直接旋转物体
- 2017.12.22
- AttributeError: module 'tensorflow' has no attribute 'select'
- JS工具类
- Capstone日志2017-12-22
- python字符串连接的三种方法及其效率、适用场景详解
- unity--3D物体的上下左右移动,鼠标控制大小,旋转
- Windows下安装Redis教程
- android studio 更新3.0后多渠道打包配置问题
- Retrofit2
- c++11 thread 类内使用方法和跨类使用方法
- synchronized同步三种使用方法
- Java基础知识:循环(1)
- CSS优先级
- html 内联类型容器中文本溢出部分隐藏