Dowemo
0 0 0 0

:
In front, mouse control visual movement article, now use the keyboard to achieve visual movement, and can let mouse and keyboard free switch to control the current.

Script as follows ( c # ):

using System.Collections;using System.Collections.Generic;using UnityEngine;publicclass Camera_move : MonoBehaviour {
 publicint speed = 5;
 public Transform bullet;
 //mouse view enumpublicenum RotationAxes
 {
 MouseXAndY = 0,
 MouseX = 1,
 MouseY =2 }
 public RotationAxes m_axes = RotationAxes.MouseXAndY;
 //鼠标敏感度publicfloat m_sensitivityX = 10f;
 publicfloat m_sensitivityY = 10f;
 //水平方向 上的 视角旋转范围publicfloat m_minimumX = -360f;
 publicfloat m_maximumX = 360f;
 //垂直方向上的 视角旋转范围publicfloat m_minimumY = -45f;
 publicfloat m_maximumY = 45f;
 float m_rotationY = 0f;
 //默认 键盘控制视角 bool isKeyView = true; 
 //Use this for initializationvoid Start () {
 //防止刚体 影响物体旋转if (GetComponent<Rigidbody>())
 {
 GetComponent<Rigidbody>().freezeRotation = true;
 }
 }
 //Update is called once per framevoid Update () {
 //根据按键 W,A,S,D 移动float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
 float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
 transform.Translate(x, 0, z);
 print("x:" + x);
 //发射子弹if (Input.GetButtonDown("Fire1"))
 {
 Transform n = Instantiate( bullet, transform.position, transform.rotation);
 //旋转方向 Vector3 front = transform.TransformDirection(Vector3.forward);
 //给物体加力 n.GetComponent<Rigidbody>().AddForce(front * 2800);
 }
 //切换 视角 ( 组合键shift + x 切换)if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.X))
 {
 isKeyView = (isKeyView == true)? false : true;
 }
 if (isKeyView)
 {
 keyControlView();
 }
 else {
 mouseControlView();
 }
 }
 void keyControlView()
 {
 //摄像机镜头(视觉) 左右转向if (Input.GetKey(KeyCode.Q))
 {
 transform.Rotate(0, -25 * Time.deltaTime, 0, Space.Self);
 }
 if (Input.GetKey(KeyCode.E))
 {
 transform.Rotate(0, 25 * Time.deltaTime, 0, Space.Self);
 }
 //摄像机镜头 上下转向if (Input.GetKey(KeyCode.Z))
 {
 transform.Rotate(-25 * Time.deltaTime, 0, 0, Space.Self);
 }
 if (Input.GetKey(KeyCode.C))
 {
 transform.Rotate(25 * Time.deltaTime, 0, 0, Space.Self);
 }
 //摄像机升高 降低if (Input.GetKey(KeyCode.H))
 {
 transform.Translate(0, 5 * Time.deltaTime, 0);
 }
 if (Input.GetKey(KeyCode.N))
 {
 transform.Translate(0, -5 * Time.deltaTime, 0);
 }
 }
 void mouseControlView()
 {
 if (m_axes == RotationAxes.MouseXAndY)
 {
 float m_rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * m_sensitivityX;
 m_rotationY += Input.GetAxis("Mouse Y") * m_sensitivityY;
 m_rotationY = Mathf.Clamp(m_rotationY, m_minimumY, m_maximumY);
 transform.localEulerAngles = new Vector3(-m_rotationY, m_rotationX, 0);
 }
 elseif (m_axes == RotationAxes.MouseX)
 {
 transform.Rotate(0, Input.GetAxis("Mouse X") * m_sensitivityX, 0);
 }
 else {
 m_rotationY += Input.GetAxis("Mouse Y") * m_sensitivityY;
 m_rotationY = Mathf.Clamp(m_rotationY, m_minimumY, m_maximumY);
 transform.localEulerAngles = new Vector3(-m_rotationY, transform.localEulerAngles.y, 0);
 }
 }
}

Mount the script on the role. It's interesting to try ~ ( ~  ̄▽ ̄ ) ~.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs