unity3d Game场景漫游脚本

using UnityEngine;
using System.Collections;

public class ScemeMove : MonoBehaviour {
	private Vector3 rota;
	public float x_speed = 50f;
	public float y_speed = 50f;
	public float m_speed = 10f;
	public float f_speed = 10f;
	public float h_speed = 1f;
	public float groundDis = 5f;
	public float skyDis = 50f;
	public Camera camera;
	// Use this for initialization
	void Start () {
		camera = Camera.main;
		rota = camera.transform.eulerAngles;
	}
	
	// Update is called once per frame
	void Update () {
		//鼠标右键
		if(Input.GetMouseButton(1)){
			rota.y += Input.GetAxis ("Mouse X") * x_speed;
			rota.x -= Input.GetAxis ("Mouse Y") * y_speed;
			Quaternion q = Quaternion.Euler (rota);
			camera.transform.rotation = q;
		}
		//滚轮滑动
		float middle = Input.GetAxis ("Mouse ScrollWheel");
	
		camera.transform.position += -Vector3.up * middle * m_speed + (Vector3.forward * middle * f_speed);
	
		//空格
		if(Input.GetKey(KeyCode.Space)){
			middle = -0.1f;
			camera.transform.position += -Vector3.up * middle * m_speed + (Vector3.forward * middle * f_speed);
		}

		//WASD
		float h = Input.GetAxis ("Horizontal");
		float v = Input.GetAxis ("Vertical");
		camera.transform.position += camera.transform.right * h * h_speed;
		camera.transform.position += camera.transform.forward * v;
		clamp ();

	}

	private void clamp(){
		Vector3 upUp;
		upUp = camera.transform.position;
		upUp.y = Mathf.Clamp (upUp.y,groundDis,skyDis);
		camera.transform.position = upUp;
	}
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页