Unity 摄像机按照中心点移动

这篇博客介绍了如何在Unity中实现摄像机按照中心点进行移动,适用于Windows和Android平台。通过鼠标或双指手势控制摄像机观察点,确保了功能的正常运行。
摘要由CSDN通过智能技术生成

场景里需要两个摄像机,两个Sprite。其中一组作为正在使用的对象,另一组作为复位的默认位置。

通过鼠标或者最多两个手指的手势可以控制摄像机观察点的移动。


以下这段代码在Windows和Android下测试通过,虽然参数不一定设置地非常恰当,不过功能已经没有问题。

using UnityEngine;
using System.Collections;

public class ControlCamera : MonoBehaviour {

    private Vector2 lastTouch1Postion = Vector2.zero;
    private Vector2 lastTouch2Postion = Vector2.zero;
    private float lastDistance = 60f;
    private float touchDistance;
    private float touch1Distance;
    private float touch2Distance;
    private float fieldOfView = DefaultFieldOfView;
    public float ScaleDump = 0.5f;
    public float RotateDump = 0.5f;
    public float TranslateDump = 0.5f;

    Vector3 lastMouseButton1Position = Vector3.zero;
    Vector3 lastMouseButton2Position = Vector3.zero;
    Vector3 lastMouseButton3Position = Vector3.zero;
    private Touch touch1;
    private Touch touch2;

    private Camera thisCamera;
    public Camera DefaultCamera;
    public GameObject DefaultCameraPoint;
    public GameObject lookAtObject;
    public bool showLookAtObject = false;

    private static Color Transparent_Color_Half = new Color(1.0f, 1.0f, 1.0f, 0.4f);
    private static Color Transparent_Color_Full = new Color(1.0f, 1.0f, 1.0f, 0.0f);
    private static float DefaultFieldOfView = 60f;

    void Start()
    {
        thisCamera = this.GetComponent<Camera>();
        lookAtObject.GetComponent<SpriteRenderer>().color = Transparent_Color_Full;
        resetCamera();
    }

    private void resetCamera()
    {
        thisCamera.transform.position = DefaultCamera.transform.position;
        thisCamera.transform.rotation = DefaultCamera.transform.rotation;
        lookAtObject.transform.position = DefaultCameraPoint.transform.position;
        lookAtObject.transform.rotation = DefaultCameraPoint.transform.rotation;
        thisCamera.transform.LookAt(lookAtObject.transform);
        lookAtObject.transform.rotation = thisCamera.transform.rotation;
        fieldOfView = DefaultFieldOfView;
        thisCamera.fieldOfView = fieldOfView;
    }

    private void DoShowLookAtObject()
    {
        if (showLookAtObject)
        {
            lookAtObject.GetComponent<SpriteRenderer>().color = Transparent_Color_Half;
        }
        else
        {
            lookAtObject.GetComponent<SpriteRenderer>().color = Transparent_Color_F
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值