首页 > 其他分享 >Unity3D轮转图(有回正效果)

Unity3D轮转图(有回正效果)

时间:2024-06-21 19:28:45浏览次数:12  
标签:Unity3D 轮转 int CreateCube float Mathf transforms 回正 movehu

注意:Main Camera 需要挂载Physics RayCaster

           Hierarchy中添加UI里面的EventSystem对象(用来相应拖拽事件)

using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Linq;

public class Rotate3D : MonoBehaviour, IDragHandler, IEndDragHandler
{
    //生成的数量
    int count = 5;
    //半径
    public int r = 3;
    //需要旋转的弧度
    float movehu = 0;
    //对象集合
    List<GameObject> gameObjects = new List<GameObject>();
    //排序集合
    List<Transform> transforms = new List<Transform>();
    //每个角的弧度
    float hu;
    // Start is called before the first frame update
    void Start()
    {
        hu = 2 * Mathf.PI / count;
        //初始化加载每个预制体到对应位置
        CreateCube();
        //默认加载第一个
        ChoseNumber(0);
    }

    private void HandleDragUpdated(float offset)
    {
        movehu = offset;
        CreateCube();

    }
    //指定去某个位置
    private void ChoseNumber(int n)
    {
        transforms = transforms.OrderBy(x => x.position.z).ToList();
        int index = gameObjects.IndexOf(transforms[0].gameObject);
        int next = index - n;
        int anext = count - Mathf.Abs(next);
        anext = next < 0 ? anext : -anext;
        float nextMove = (Mathf.Abs(next) < Mathf.Abs(anext) ? next : anext) * hu;
        float nextAng = Mathf.Asin(transforms[0].position.x / r) + nextMove;
        DT.To((c) =>
        {
            movehu = c;
            CreateCube();
        }, movehu, movehu + nextAng, Mathf.Abs(nextAng));
    }

    private void CreateCube()
    {
        for (int i = 0; i < count; i++)
        {
            if (gameObjects.Count <= i)
            {
                //加载需要加载的预制体
                GameObject obj = GameObject.Instantiate(Resources.Load<GameObject>("player/player_" + i), transform);
                obj.transform.Rotate(Vector3.up * 180);
                gameObjects.Add(obj);
                transforms.Add(gameObjects[i].transform);
            }
            float x = Mathf.Sin(hu * i + movehu) * r;
            float y = Mathf.Cos(hu * i + movehu) * r;
            gameObjects[i].transform.position = new Vector3(x, 0, y);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        var dis = eventData.delta.x / (r * 100);
        movehu -= dis;//控制方向
        CreateCube();
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        var dis = eventData.delta.x / 100;
        DT.To((a) =>
        {
            movehu -= a / r;
            CreateCube();
        }, dis, 0, Mathf.Abs(dis / r)).OnComplete(() =>
        {
            transforms = transforms.OrderBy(x => x.position.z).ToList();
            float zhenghu = Mathf.Asin(transforms[0].position.x / r);
            DT.To((b) =>
            {
                movehu = b;
                CreateCube();
            }, movehu, movehu + zhenghu, 1);
        });
    }
}

DO.To() 方法参考我作品中的Unity手写模拟DoTween中的To功能 也可以直接使用DoTween包

标签:Unity3D,轮转,int,CreateCube,float,Mathf,transforms,回正,movehu
From: https://blog.csdn.net/djrjnfjdjejb/article/details/139868212

相关文章

  • Unity3D 八叉树划分空间和可视化
    也许更好的阅读体验成果展示代码OctreeNodeusingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassOctreeNode{//空间内包含的物体publicList<GameObject>areaObjects;//空间中心publicVector3center;......
  • Unity3D 用贝塞尔曲线进行弹道追踪
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnity.Collections.LowLevel.Unsafe;usingUnityEngine;usingUnityEngine.UI;publicclassBulletLogic:MonoBehaviour{//Startiscalledbeforethefirstframeupdate//飞行速度最大......
  • 【名词解释】Unity3D物理射线(Physics Ray)含义及其用法
    Unity3D是一款流行的游戏开发引擎,它提供了一套强大的工具和功能来帮助开发者创建交互式3D内容。在Unity中,"物理射线"(PhysicsRay)通常指的是使用射线检测(Raycasting)来检测物体之间的碰撞或者检测射线与物体的交点。这在游戏开发中非常常见,用于实现如射击、视线检测、物体碰撞检......
  • 【C/C++】实现高性能日志轮转功能,已实测
    基本实现在C语言中实现日志文件轮转功能,你需要手动编写代码来处理文件的重命名、压缩和删除。下面是一个简单的C语言程序示例,它演示了如何实现基本的日志文件轮转功能。这个程序会检查日志文件的大小,如果超过预设的大小限制,则将当前日志文件重命名,并创建一个新的日志文件。......
  • CPU 时间片轮转机制
    CPU时间片轮转机制我们平时在开发的时候,感觉并没有受cpu核心数的限制,想启动线程就启动线程,哪怕是在单核CPU上,为什么?这是因为操作系统提供了一种CPU时间片轮转机制。时间片轮转调度是一种最古老、最简单、最公平且使用最广的算法,又称RR调度。每个进程被分配一个时间段,......
  • C语言王国——数组的旋转(轮转数组)三种解法
    一、题目给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。示例1:输入:nums=[1,2,3,4,5,6,7],k=3输出:[5,6,7,1,2,3,4]解释:向右轮转1步:[7,1,2,3,4,5,6]向右轮转2步:[6,7,1,2,3,4,5]向右轮转3步:[5,6,7,1,2,3,4]示例......
  • js实现歌词自动轮转及播放时间跳转
    需求实现歌词滚动提示和歌词点击跳转功能思路歌词滚动:将歌词解析成两个数组,一个用于存放时间,一个用于存放歌词,下标一致对应,利用定时器对每毫秒轮询时间数组,若一致,显示歌词变红。歌词点击播放:判断点击歌词在数组中的下标,读取时间数组中的时间,赋值播放时间代码歌词滚动.html<......
  • 力扣-189. 轮转数组
    1.题目题目地址(189.轮转数组-力扣(LeetCode))https://leetcode.cn/problems/rotate-array/题目描述给定一个整数数组nums,将数组中的元素向右轮转k 个位置,其中 k 是非负数。 示例1:输入:nums=[1,2,3,4,5,6,7],k=3输出:[5,6,7,1,2,3,4]解释:向右轮转1步......
  • Unity3D 打造基于AStar的寻路与导航详解
    Unity3D打造基于AStar的寻路与导航详解BYCW丶零夜 ​关注 2人赞同了该文章前言寻路与导航是游戏开发中非常重要的一部分,它可以让游戏中的角色自动寻找到目标位置,并避开障碍物。本文将介绍如何使用Unity3D打造基于AStar算法的寻路与导航解,包括技术详......
  • Unity3D代码混淆方案详解
    背景Unity引擎使用Mono运行时,而C#语言易受反编译影响,存在代码泄露风险。本文通过《QQ乐团》项目实践,提出一种适用于Unity引擎的代码混淆方案,以保护代码逻辑。引言在Unity引擎下,为了防止代码被轻易反编译,需要采取相应的保护措施。本文将分享一种基于实践经验的可行方案,希......