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

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

using UnityEngine;
using System.Collections;
 
public class CameraControl : MonoBehaviour {
	// Use this for initialization
	private GameObject gameObject;
	float x1;
	float x2;
	float x3;
	float x4;
	void Start () {
		gameObject = GameObject.Find ("Main Camera");//对应摄像机名称
	}
	
	// Update is called once per frame
	void 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 initialization
    void Start()
    {
        CammeraMain = Camera.main.transform;
    }

    // Update is called once per frame
    void 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);
        }
        

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值