1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Unity3D鼠标 WASD空格键盘控制摄像机及esc键退出C#脚本

Unity3D鼠标 WASD空格键盘控制摄像机及esc键退出C#脚本

时间:2023-03-02 02:33:49

相关推荐

Unity3D鼠标 WASD空格键盘控制摄像机及esc键退出C#脚本

C#控制WASD键盘前后左右及空格键抬升高度脚本代码如下:

using UnityEngine;using System.Collections;public class CameraControl : MonoBehaviour {// Use this for initializationprivate GameObject gameObject;float x1;float x2;float x3;float x4;void Start () {gameObject = GameObject.Find ("Main Camera");//对应摄像机名称}// Update is called once per framevoid Update () {//空格键抬升高度if (Input.GetKey (KeyCode.Space)){transform.position = new Vector3(transform.position.x,transform.position.y + 1,transform.position.z);} //w键前进if(Input.GetKey(KeyCode.W)){this.gameObject.transform.Translate(new Vector3(0,0,50*Time.deltaTime));}//s键后退if(Input.GetKey(KeyCode.S)){this.gameObject.transform.Translate(new Vector3(0,0,-50*Time.deltaTime));}//a键左移if(Input.GetKey(KeyCode.A)){this.gameObject.transform.Translate(new Vector3(-10,0,0*Time.deltaTime));}//d键右移if(Input.GetKey(KeyCode.D)){this.gameObject.transform.Translate(new Vector3(10,0,0*Time.deltaTime));}}}

C#控制鼠标移动、转向及esc键退出脚本代码如下:

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;public class CameraMove2 : MonoBehaviour{Transform CammeraMain;//滑轮滚动基数public float WheelMove = 0;//鼠标每帧移动基数public float MouseMove_X = 0;public float MouseMove_Y = 0;//相机移动灵敏度public float Intensity = 1000f;//方向键控制相关参数int speed = 50;float n = 200.8f;//相机自身X/Y轴方向Vector3 X_Aix = new Vector3(1, 0, 0);Vector3 Y_Aix = new Vector3(0, 1, 0);//相机x轴在水平面投影Vector3 HX_Aix = new Vector3(1, 0, 0);//世界坐标Y轴Vector3 WY_Aix = new Vector3(0, 1, 0);//绕某一点旋转Vector3 RotatePoint = Vector3.zero;// Use this for initializationvoid Start(){CammeraMain = Camera.main.transform;}// Update is called once per framevoid Update(){//退出ESC事件if (Input.GetKeyDown(KeyCode.Escape)) {Application.Quit();}//更新相机自身X/Y轴方向X_Aix = CammeraMain.right;Y_Aix = CammeraMain.up;//更新旋转点(相机正前方3米)RotatePoint = (CammeraMain.forward).normalized * 3 + CammeraMain.position;//鼠标左键 摄像机绕某点旋转if (Input.GetMouseButton(1)){if ((MouseMove_X = Input.GetAxis("Mouse X")) != 0){CammeraMain.RotateAround(RotatePoint, WY_Aix, MouseMove_X);}if ((MouseMove_Y = Input.GetAxis("Mouse Y")) != 0){CammeraMain.RotateAround(RotatePoint, X_Aix, -MouseMove_Y);}}//滑轮滚动 摄像机前后移动if ((WheelMove = Input.GetAxis("Mouse ScrollWheel")) != 0){CammeraMain.Translate(0, 0, WheelMove * 500);}//摄像机在世界坐标下的水平移动if (Input.GetMouseButton(0)){CammeraMain.Translate(MouseMove_X = -Input.GetAxis("Mouse X") * Intensity, MouseMove_X = -Input.GetAxis("Mouse Y") * Intensity, 0);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。