CameraControl

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CameraControl : MonoBehaviour
{
    public Transform center;
    private Vector3 Center;

    //ScrollWheel
    private int MouseWheelSensitivity = 2;
    private int MouseZoomMin = 1;
    private int MouseZoomMax = 200;
    public float normalDistance = 6;
    private Vector3 normalized;
    private Vector3 temp = new Vector3(0,0,0);

    //Rotation
    private float xSpeed = 250.0f;
    private float ySpeed = 120.0f;
    private int yMinLimit = 0;
    private int yMaxLimit = 80;
    public bool needDamping = false; //调整摄像机
    private float damping = 5.0f;

    private float x = 0.0f;
    private float y = 0.0f;

    private float movingSpeed = 0.5f;

    private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));

    void Start()
    {
        Center = center.position;

        float z = center.transform.position.z - normalDistance;
        //transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);
        transform.LookAt(center);

        var angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }

    void LateUpdate()
    {
        Center = center.position;
        //左键旋转
        if (Input.GetMouseButton(0)) RotationCamera();
        //滚轮缩放
        else if (Input.GetAxis("Mouse ScrollWheel") != 0) MouseWheel();
        //中键平移
        //else if (Input.GetMouseButton(2)) HVMoveCamera();
    }
     float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
     public void RotationCamera()
    {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + Center;
        if (needDamping)
        {
            transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
            transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
        }
        else
        {
            transform.rotation = rotation;
            transform.position = position;
        }
    }
     public void MouseWheel()
     {
         normalized = (Center - transform.position).normalized;
         if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
         {
             normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
             temp = Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity * normalized;
         }
         if (normalDistance < MouseZoomMin)
         {
             normalDistance = MouseZoomMin;
             temp.Set(0, 0, 0);
         }
         if (normalDistance > MouseZoomMax)
         {
             normalDistance = MouseZoomMax;
             temp.Set(0, 0, 0);
         }
         transform.position += temp;
     }
     public void HVMoveCamera()
     {
         x = Input.GetAxis("Mouse X") * movingSpeed;
         y = Input.GetAxis("Mouse Y") * movingSpeed;
         rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
         transform.position = rotation * new Vector3(x, 0, y) + transform.position;
     }
    //模拟单击事件
     public void TranslateCamera(Vector3 cen)
     {
         x = cen.x;
         y = cen.y;
         y = ClampAngle(y, yMinLimit, yMaxLimit);

         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + Center;
         if (needDamping)
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
             transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
         }
         else
         {
             transform.rotation = rotation;
             transform.position = position;
         }
     }
}
时间: 2024-10-13 07:17:52

CameraControl的相关文章

在DevExpress中使用CameraControl控件进行摄像头图像采集

在我们以前的项目了,做摄像头的图片采集,我们一般还是需要做一个封装处理的,在较新版本的DevExpress控件里面,增加了一个CameraControl控件,可以直接调用摄像头显示的,因此也可以做头像采集等功能,本文介绍如何基于这个控件做相关的图像采集操作. 1.CameraControl控件介绍 该控件从15.1开始,就开始加入控件组了,控件的使用很简单,直接拖动到界面就可以在Winform界面上使用了.本案例基于16.1进行开发的,因此也都具有这些控件模块的. 例如我们直接把CameraCo

控制模型展示视角

  1 using UnityEngine; 2 using System.Collections; 3 4 public class CameraControl : MonoBehaviour { 5 public Transform target; 6 public float distance = 5f;//缩放系数 7 public float mixdistance = 2;//摄像机离物体的最近距离,越小放大倍数越大 8 public float maxdistance = 10;/

猫跳窗户 学习

1 using UnityEngine; 2 using System.Collections; 3 4 /// <summary> 5 /// 镜头控制 6 /// </summary> 7 public class CameraControl : MonoBehaviour { 8 9 /// <summary> 10 /// 玩家 11 /// </summary> 12 private GameObject player = null; 13 ///

Tanks!Tutorial 学习

using UnityEngine; namespace Complete { public class CameraControl : MonoBehaviour { /// <summary> /// 相机重新聚焦的时间 /// </summary> public float m_DampTime = 0.2f; /// <summary> /// /// </summary> public float m_ScreenEdgeBuffer = 4f;

Unity之一天一个技术点(十三)

var target : Transform; var distance = 10.0; var xSpeed = 250.0; var ySpeed = 120.0; var yMinLimit = -20; var yMaxLimit = 80; private var x = 0.0; private var y = 0.0; @AddComponentMenu("Camera-Control/Mouse Orbit") partial class MouseOrbit { }

转:Oculus Unity Development Guide开发指南(2015-7-21更新)

http://forum.exceedu.com/forum/forum.php?mod=viewthread&tid=34175 Oculus Unity Development Guide开发指南转载请保留原始地   http://t.cn/RAblKoh Oculus/GearVR开发者群 302294234 Welcometo the Unity Development GuideIntroduction简介Welcometo the Oculus Unity Developer Gui

C#版官方角色控制器脚本

将官方自带的角色控制器脚本,改写为C#版,以下为所有代码: CharacterMotor.cs  主要设置角色控制的系数,如运动,跳跃,移动,滑动等.第一人称与第三人称主角模型的移动与旋转的角度最后都是在这里计算的. CharacterMotor.cs  代码: 1 using UnityEngine; 2 using System.Collections; 3 4 /** 5 *主要设置角色控制的系数,如运动,跳跃,移动,滑动等.第一人称与第三人称主角模型的移动与旋转的角度最后都是在这里计算的

unity3d 学习 1

编写目的:unity 学习 编写时间:2014-08-26 02:13 发射器 using UnityEngine; using System.Collections; //声明 有属性将在检视面板出现 [AddComponentMenu("Camera-Control/Shooter")] [System.Serializable] public class Shooter : MonoBehaviour { public Transform bullet; public float

Unity3d 性能优化遐想 OnBecameVisible/OnBecameInvisible

当玩家在主城中行走时,周围大量玩家显示在玩家周围,不管是玩家能看到的前方,还是看不到的后方,所有的人物模型都在进行着脚本运算. 在OpenGL中 提供了裁剪功能,不在视口范围内的点和面会被裁剪掉以减少渲染代价,在Unity中也顺势提供了相关的API,提供物体进入/离开 视口的函数回调. 文章转自http://blog.csdn.net/huutu   http://www.thisisgame.com.cn OnBecameVisible/OnBecameInvisible 当物体进入视口,触发