找回密码
 立即注册
查看: 304|回复: 0

unity 模型旋转与放大缩小

[复制链接]
发表于 2022-5-5 14:58 | 显示全部楼层 |阅读模式
版本unity2020.3.31
一、安卓手机实现手指滑动旋转模型
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{

    //上一个手指的位置
    //Vector3 PrevlousPosition;
    //手指的偏移值
    //Vector3 Offset;

    //速度
    float xSpeed = 150f;

    void Start()
    {

    }

    void Update()
    {

        //如果触摸了屏幕
        if (Input.GetMouseButton(0))
        {
            //判断是几个手指触摸
            if (Input.touchCount == 1)
            {
                //第一个触摸到手指头 phase状态 Moved滑动
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    //根据你旋转的 模型物体 是要围绕哪一个轴旋转 Vector3.up是围绕Y旋转
                    transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime);
                }
            }
        }
    }
}
二、安卓手机实现放大缩小模型
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//放大缩小功能 f
public class Scale : MonoBehaviour
{

    Vector2 oldPos1;
    Vector2 oldPos2;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount == 2)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                Vector2 temPos1 = Input.GetTouch(0).position;
                Vector2 temPos2 = Input.GetTouch(1).position;

                if (isEnLarge(oldPos1, oldPos2, temPos1, temPos2))
                {
                    float oldScale = transform.localScale.x;
                    //放大的倍数 如果要修改 列如:oldScale * 1.025f 修改成 oldScale * 1.25f 根据自己需求修改
                    float newScale = oldScale * 1.025f;

                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                else
                {
                    float oldScale = transform.localScale.x;
                    //缩小的倍数 如果要修改 列如:oldScale / 1.025f 修改成 oldScale / 1.25f 根据自己需求修改
                    float newScale = oldScale / 1.025f;

                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }

                oldPos1 = temPos1;
                oldPos2 = temPos2;

            }
        }
    }

    bool isEnLarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {

        float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

        if (length1 < length2)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
}
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 13:44 , Processed in 0.166994 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表