- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38268
- 精华
- 111
|
控制摄像机代码
- #pragma strict
- var firsttarget : Transform;
- var enemytarget : Transform;
- var target : Transform; //摄像机需要围绕的目标的坐标
- var distance=15.0; // 摄像机距离该目标的起始距离
- var targetcomfirm = 0;
- var xSpeed = 250; //X轴 旋转速度
- var ySpeed = 100; //Y轴旋转速度
- var yMinLimit = -20; //Y轴最小限制
- var yMaxLimit = 80; //Y轴最大限制
- var x = 0.0; //初始值
- var y= 0.0;
- //摄像机移动
- var xMove= 0.0;
- var yMove= 0.0;
- var distanceMove=20;
- //战略按钮
- var viewImage :Texture;
- var viewStyle :GUIStyle;
- private var oldPosition1 : Vector2; //记录触摸点上次位置
- private var oldPosition2 : Vector2;
- private var oldPosition3 : Vector2; //记录触摸点上次位置
- private var oldPosition4 : Vector2;
- var hits : RaycastHit[];
- var ray : Ray;
- var IdentityCheat: Identity;
- //LayerMask mask = 1 << 8;
- var smoothSwitch : int;
- function Start () {
- var angles = transform.eulerAngles; //一种以欧拉角度量的旋转。transform.eulerAngles(x,y,z);表示绕x,y,z分别旋转的度数。
- x= angles.y;
- y= angles.x;
- transform.rotation = Quaternion.Euler(90, 0, 0);
- }
- function Update () {
-
- if(target){
- //检测单点触摸
- if(Input.touchCount == 1){
-
- if(Input.GetTouch(0).phase==TouchPhase.Moved){ //检测手指在划过屏幕
- x += Input.GetAxis("Mouse X") * xSpeed * 0.02; //通过轴输入来控制
- y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
- }
- else {
- // 触屏产生射线控制物体
- ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
- hits = Physics.RaycastAll (ray, 100);
- firsttarget=hits[0].transform;
- IdentityCheat=firsttarget.GetComponent(Identity);
- if(IdentityCheat.player==1){
- target=firsttarget;
- smoothSwitch=1;
- var cintrolSystem=target.GetComponent(SpaceShip);
- cintrolSystem.controlActive();
- }
- else if(IdentityCheat.tag=="enemy"){
- enemytarget=firsttarget;
- targetcomfirm=1;
-
-
- }
-
-
-
- }
- }
- //检测双点触摸
- else if(Input.touchCount >1){
- if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved){
- var tempPosition1 = Input.GetTouch(0).position; //计算出当前两点触摸点的位置
- var tempPosition2 = Input.GetTouch(1).position;
- if(isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2)){//函数返回真为放大,返回假为缩小
-
- if(distance>3)//放大系数超过3以后不允许继续放大
- distance -=0.2;
-
- }
- else{
- if(distance<100) //缩小系数超过50后不允许继续缩小
- distance += 0.2;
-
- }
- //备份上一次触摸点的位置,用于对比
- oldPosition1=tempPosition1;
- oldPosition2=tempPosition2;
-
-
- }
- }
- }
- //视角移动
- else{
- if(Input.touchCount == 1){
-
- if(Input.GetTouch(0).phase==TouchPhase.Moved){ //检测手指在划过屏幕
- xMove -= Input.GetAxis("Mouse X") *0.2; //通过轴输入来控制
- yMove -= Input.GetAxis("Mouse Y") *0.2;
- }
- else {
- // 触屏产生射线控制物体
- ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
- hits = Physics.RaycastAll (ray, 100);
- firsttarget=hits[0].transform;
- IdentityCheat=firsttarget.GetComponent(Identity);
- if(IdentityCheat.player==1){
- target=firsttarget;
- smoothSwitch=1;
- cintrolSystem=target.GetComponent(SpaceShip);
- cintrolSystem.controlActive();
- }
-
-
- }
- }
- /*else if(Input.touchCount >1){
- if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved){
- var tempPosition3 = Input.GetTouch(0).position; //计算出当前两点触摸点的位置
- var tempPosition4 = Input.GetTouch(1).position;
- if(isEnlarge(oldPosition3,oldPosition4,tempPosition3,tempPosition4)){//函数返回真为放大,返回假为缩小
-
- if(distanceMove>3)//放大系数超过3以后不允许继续放大
- distanceMove -=0.2;
-
- }
- else{
- //缩小系数超过50后不允许继续缩小
- distanceMove += 0.2;
-
- }
- //备份上一次触摸点的位置,用于对比
- oldPosition3=tempPosition3;
- oldPosition4=tempPosition4;
-
-
- }
- } */
-
-
-
- }
-
- if(distanceMove<3)
- distanceMove=3;
- }
-
-
- function isEnlarge(oP1 : Vector2,oP2 : Vector2,nP1 : Vector2,nP2 : Vector2) : boolean{
- //计算两个触摸点的距离,进行对比, 第一个距离比第二个距离大则是缩小,反之为放大
- var leng1 =Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y));
- var leng2 =Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y));
- if(leng1<leng2){
- return true;
- }
- else
- {
- return false;
- }
-
- }
- function LateUpdate () {
-
- //target为我们绑定的箱子变量,缩放旋转的参照物
- if (target) {
-
- //重置摄像机的位置,始终以目标为中心
- y = ClampAngle(y, yMinLimit, yMaxLimit);
- var rotation = Quaternion.Euler(y, x, 0); //绕x ,y轴 旋转相应的角度
- var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
- //var position= rotation * Vector3(0.0, 0.0, -distance) +Vector3(Mathf.Lerp(oldCameraPosition.x,target.position.x, Time.time * 1),target.position.y,target.position.z);
-
- // transform.rotation = Quaternion.Slerp(transform.rotation, rotation,Time.deltaTime * 2);//调整摄像机位置 角度渐变
- transform.rotation = rotation;
- if(smoothSwitch){
- transform.position = Vector3.Lerp(transform.position,position, Time.deltaTime * 2);
- smoothSwitch=0;
-
- }
- else
- transform.position=position;
-
-
-
- }
- //无目标 屏幕移动
- else{
- var Moveposition = rotation * Vector3(xMove, distanceMove, yMove) ;
- transform.position = Vector3.Lerp(transform.position,Moveposition, Time.time);
-
- }
-
-
-
-
-
- }
- static function ClampAngle (angle : float, min : float, max : float) {
- //规正角度
- if (angle < -360)
- angle += 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp (angle, min, max);
- }
- function OnGUI(){
-
- if(GUI.Button(Rect(Screen.width*0.9,Screen.height*0.8,100,100),viewImage,viewStyle))
- {
-
- var newposition = target.transform.position+Vector3(0,30,0);
- //camera.orthographic = true;
- transform.rotation = Quaternion.Euler(90, 0, 0);
-
- xMove=transform.position.x;
- distanceMove=30 ;
- yMove=transform.position.z;
- target=null;
-
-
- }
-
- }
复制代码 |
|