这次文章主要是看了网上写的很多关于物体上下左右位移,鼠标控制物体的大小,旋转问题,但是下载很多,依然没有自己想要的结果,就自己根据前辈经验,在上面稍加修改,简直就是班门弄斧了。方便自己,也方便大家.这次主要有几点需要说明,就是里面的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));
//}
}