【转】摄像机漫游脚本

  1 //-----------------------------------------------------------------  
  2 //1,把本类作为一个组件,包含在 GameObject 中。  
  3 //2,左手坐标系。  
  4 //-----------------------------------------------------------------  
  5 using System.Collections;
  6 using System.Collections.Generic;
  7 using UnityEngine;
  8 //-----------------------------------------------------------------  
  9 public class CameraControl : MonoBehaviour
 10 {
 11     public float moveSpeed = 8.0f;
 12     public float rotateSpeed = 0.05f;
 13 
 14 
 15     public static Vector3 kUpDirection = new Vector3(0.0f, 1.0f, 0.0f);
 16 
 17 
 18     //控制摄像机旋转的成员变量。  
 19     private float m_fLastMousePosX = 0.0f;
 20     private float m_fLastMousePosY = 0.0f;
 21     private bool m_bMouseRightKeyDown = false;
 22 
 23 
 24     //-----------------------------------------------------------------  
 25     void Start()
 26     {
 27 
 28 
 29     }
 30     //-----------------------------------------------------------------  
 31     void Update()
 32     {
 33         //判断旋转  
 34         if (Input.GetMouseButtonDown(1)) //鼠标右键刚刚按下了  
 35         {
 36             if (m_bMouseRightKeyDown == false)
 37             {
 38                 m_bMouseRightKeyDown = true;
 39                 Vector3 kMousePos = Input.mousePosition;
 40                 m_fLastMousePosX = kMousePos.x;
 41                 m_fLastMousePosY = kMousePos.y;
 42             }
 43         }
 44         else if (Input.GetMouseButtonUp(1)) //鼠标右键刚刚抬起了  
 45         {
 46             if (m_bMouseRightKeyDown == true)
 47             {
 48                 m_bMouseRightKeyDown = false;
 49                 m_fLastMousePosX = 0;
 50                 m_fLastMousePosY = 0;
 51             }
 52         }
 53         else if (Input.GetMouseButton(1)) //鼠标右键处于按下状态中  
 54         {
 55             if (m_bMouseRightKeyDown)
 56             {
 57                 Vector3 kMousePos = Input.mousePosition;
 58                 float fDeltaX = kMousePos.x - m_fLastMousePosX;
 59                 float fDeltaY = kMousePos.y - m_fLastMousePosY;
 60                 m_fLastMousePosX = kMousePos.x;
 61                 m_fLastMousePosY = kMousePos.y;
 62 
 63 
 64                 Vector3 kNewEuler = transform.eulerAngles;
 65                 kNewEuler.x += (fDeltaY * rotateSpeed);
 66                 kNewEuler.y += -(fDeltaX * rotateSpeed);
 67                 transform.eulerAngles = kNewEuler;
 68             }
 69         }
 70 
 71 
 72         //判断位移  
 73         float fMoveDeltaX = 0.0f;
 74         float fMoveDeltaZ = 0.0f;
 75         float fDeltaTime = Time.deltaTime;
 76         if (Input.GetKey(KeyCode.A))
 77         {
 78             fMoveDeltaX -= moveSpeed * fDeltaTime;
 79         }
 80         if (Input.GetKey(KeyCode.D))
 81         {
 82             fMoveDeltaX += moveSpeed * fDeltaTime;
 83         }
 84         if (Input.GetKey(KeyCode.W))
 85         {
 86             fMoveDeltaZ += moveSpeed * fDeltaTime;
 87         }
 88         if (Input.GetKey(KeyCode.S))
 89         {
 90             fMoveDeltaZ -= moveSpeed * fDeltaTime;
 91         }
 92         if (fMoveDeltaX != 0.0f || fMoveDeltaZ != 0.0f)
 93         {
 94             Vector3 kForward = transform.forward;
 95             Vector3 kRight = Vector3.Cross(kUpDirection, kForward);
 96             Vector3 kNewPos = transform.position;
 97             kNewPos += kRight * fMoveDeltaX;
 98             kNewPos += kForward * fMoveDeltaZ;
 99             transform.position = kNewPos;
100         }
101     }
102 }

原链接:http://www.cnblogs.com/oilcode/p/6927951.html

posted @ 2017-07-24 16:25  露夕逝  阅读(370)  评论(0编辑  收藏  举报