Unity 控制摄像机的移动,旋转,缩放 模仿Scene场景控制方式

共两个脚本,挂载主摄像机即可。
1、W,A,S,D,Space 控制逻辑

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));
		}
	}
}



2、鼠标方向 控制逻辑

using UnityEngine;

public class FirstPersonalCamera : MonoBehaviour
{
    public float speed = 5;
    // Update is called once per frame
    void Update()
    {
        
        //鼠标在这一帧移动的水平距离
        float x = Input.GetAxis("Mouse X");
        //绕世界坐标中的y轴旋转
        transform.Rotate(Vector3.up * x * speed, Space.World);
        //鼠标在这一帧移动的垂直距离
        float y = Input.GetAxis("Mouse Y");
        //绕自身的x轴转
        transform.Rotate(Vector3.right * -y * speed);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38803654/article/details/107243947