Dowemo

:
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;



public class Camera_move : MonoBehaviour {



 public int speed = 5;


 public Transform bullet;



 //mouse view enum


 public enum RotationAxes


 {


 MouseXAndY = 0,


 MouseX = 1,


 MouseY =2


 }



 public RotationAxes m_axes = RotationAxes.MouseXAndY;


 //鼠标敏感度


 public float m_sensitivityX = 10f;


 public float m_sensitivityY = 10f;


 //水平方向 上的 视角旋转范围


 public float m_minimumX = -360f;


 public float m_maximumX = 360f;


 //垂直方向上的 视角旋转范围


 public float m_minimumY = -45f;


 public float m_maximumY = 45f;



 float m_rotationY = 0f;


 //默认 键盘控制视角 


 bool isKeyView = true; 



 //Use this for initialization


 void Start () {


 //防止刚体 影响物体旋转


 if (GetComponent<Rigidbody>())


 {


 GetComponent<Rigidbody>().freezeRotation = true;


 }


 }



 //Update is called once per frame


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


 }


 else if (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