Unity通过鼠标或者手势实现拉进拉远,旋转等操作的常用方法

本文介绍了在Unity项目中实现镜头拉伸和旋转的两种方法。一种是通过调整camera与目标距离及setpos方法更新位置,适用于三维模型;另一种是通过改变物体scale进行放大缩小,适用于二维平面场景。
摘要由CSDN通过智能技术生成

在项目制作过程中,我们可能会用到镜头的拉伸与旋转操作,不管在pc端还是移动端,拉伸效果要么就是放大target,要么就是移动camera,旋转也是一样,要么旋转摄像机,要么是旋转物体本身,具体情况具体对待。下面就两个方法的具体实现方法:


下面是通过控制camera与target的距离,实时更新camera的位置来实现物体的方法缩小,也就是拉伸效果。同样旋转物体也是通过setpos方法,实时更新位置实现摄像机的旋转。这种拉伸效果适合用在target是三维模型时。

using UnityEngine;
using System.Collections;

public class RotateAndPinch_2 : MonoBehaviour
{

    public Transform target;
    private float minVertical = 0f;
    private float maxVertical = 85f;
    private float x = 0.0f;
    private float y = 0.0f;
    private float distance = 0.0f;

    private float newdis = 0;
    private float olddis = 0;
    // Use this for initialization
    void Start()
    {
        distance = (transform.position - target.position).magnitude;
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(target);
        float dt = Time.deltaTime;
        x = transform.eulerAngles.y;
        y = transform.eulerAngles.x;

        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {

            if (Input.touchCount == 1)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    float x1 = Input.GetAxis("Mouse X");
                    float y1 = Input.GetAxis("Mouse Y");
                    x += x1 * dt * 15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值