首页 > 其他分享 >【Unity】旋转木马

【Unity】旋转木马

时间:2023-04-22 12:58:01浏览次数:42  
标签:Mathf horseNodes float pos 旋转 owenRotateCyc Unity 木马 new

对三角函数进行实际操作,需要对木马移动进行平滑插值 木马起伏采用的Cos函数的周期实现

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

public class MerryGoRound : MonoBehaviour
{
    public Transform node;

    public float rudis;

    public float viry;

    private Transform[] horseNodes;

    private float[] owenRotateCyc;

    private float rotateGap;

    void Awake() {
        Application.targetFrameRate = 60;    
    }

    // Start is called before the first frame update
    void Start()
    {
        horseNodes = new Transform[node.childCount];
        owenRotateCyc = new float[node.childCount];
        rotateGap = 360 / horseNodes.Length;
        nextMoveYs = new float[owenRotateCyc.Length];
        lastYs = new float[owenRotateCyc.Length];
        for (int i = 0; i < node.childCount; i++)
        {
            var zhuzi = node.GetChild(i);
            horseNodes[i] = zhuzi.GetChild(0);

            owenRotateCyc[i] = (180 * i - 1) % 360 + 1;
            float rad = Mathf.Deg2Rad * rotateGap * i;
            var pos = new Vector3(rudis * Mathf.Cos(rad), 0, rudis * Mathf.Sin(rad));
            zhuzi.localPosition = new (pos.x, 0, pos.z);
            zhuzi.localRotation = Quaternion.Euler(0, - rotateGap * i + 90, 0);
            
            horseNodes[i].localPosition = new (0, Mathf.Cos(Mathf.Deg2Rad * owenRotateCyc[i]) * viry, 0);
            nextMoveYs[i] = horseNodes[i].localPosition.y;
        }

        lerpRate = lerpRate / cycTime;
    }

    float timer = 0.21f;
    float[] nextMoveYs;
    float[] lastYs;
    float lerpTimer;

    float cycTime = 0.21f;
    float lerpRate = 1f;
    // Update is called once per frame
    void Update()
    {
        if(timer >= cycTime)
        {
            timer = 0;
            for (int i = 0; i < owenRotateCyc.Length; i++)
            {

                owenRotateCyc[i] = (owenRotateCyc[i] + 5 - 1) % 360 + 1;
                var pos = horseNodes[i].localPosition;
                pos.y = nextMoveYs[i];
                horseNodes[i].localPosition = pos;
                lastYs[i] = pos.y;
                nextMoveYs[i] = Mathf.Cos(Mathf.Deg2Rad * owenRotateCyc[i]) * viry;
            }
            lerpTimer = 0f;
        }

        lerpTimer += Time.deltaTime * lerpRate;
        node.Rotate(node.up, Time.deltaTime * lerpRate, Space.World);

        for (int i = 0; i < owenRotateCyc.Length; i++)
        {
            float y = Mathf.Lerp(lastYs[i], nextMoveYs[i], lerpTimer);
            var pos = horseNodes[i].localPosition;
            pos.y = y;
            horseNodes[i].localPosition = pos;
        }
        
        timer += Time.deltaTime;
    }
}

Package链接

标签:Mathf,horseNodes,float,pos,旋转,owenRotateCyc,Unity,木马,new
From: https://www.cnblogs.com/itcod/p/17342781.html

相关文章

  • iOS 屏幕旋转的设置方法
    VC上屏幕旋转的方式有2种1.因重力导致的屏幕旋转条件:shouldAutorotate返回true,设备开启了屏幕旋转开关。设备发生重力旋转。2.单页面强制旋转条件:无。设置设备旋转方向。NSNumber*orientationTarget=[NSNumbernumberWithInteger:isLaunchScreen?UIInterfaceOrient......
  • unity实现简单AR识别
    首先前往unity官网:https://unity.cn/releases,下拉找到下载UnityHub绿色按钮下载完安装进入UnityHub中,在侧边栏找到Installs,点击打开下载如图所示的unity版本(注:unity同ue,只能高版本向低版本兼容)下载过程中记得取消勾选Documentation,选择简体中文(注:很多内容需要访问外网......
  • Unity Resources.Load
    图片路径必须是Assets\Resources目录下面的,并且不能带扩展名//E:\Assets\Resources\img\abc.jpgstringfilePath="img/abc";vartexture=Resources.Load<Texture2D>(filePath);GameObjectobj=newGameObject("newname",typeof(SpriteRenderer));Spr......
  • 二分查找:剑指 Offer 11. 旋转数组的最小数字
    题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。例如,数组 [3,4,5,1,2]为[1,2,3,4,5]的一次旋转,该数组的最......
  • #yyds干货盘点# LeetCode面试题:搜索旋转排序数组 II
    1.简述:已知存在一个按非降序排列的整数数组nums,数组中的值不必互不相同。在传递给函数之前,nums在预先未知的某个下标k(0<=k<nums.length)上进行了旋转,使数组变为[nums[k],nums[k+1],...,nums[n-1],nums[0],nums[1],...,nums[k-1]](下标从0开始计数)。例如,[0,1,2......
  • Unity- 数据与网络(占坑)
    一、常用数据格式1.轻量数据格式JSON1.1对象1.2数组1.3SON的创建和解析2.可扩展标记语言XML1.使用XML语法进行解析2.使用XPath语法进行解析CSV与ExcelSQLite1.创建表2.删除表3.增加数据4.查找数据5.删除数据6.修改数据二、多线程三、网络请求的使用网络请求搭......
  • 在MacOS下使用Unity3D开发游戏
    第一次发博客,先发一下我的游戏开发环境吧。去年2月份买了一台MacBookPro2021M1pro(以下简称mbp),这一年来一直在用mbp开发游戏。我大致分享一下我的开发工具以及使用体验。1、Unity官网链接:https://unity.cn/releases我一般使用的Applesilicon版本的,支持M1芯片,无需转译。Un......
  • 矩阵左乘与右乘和旋转矩阵、坐标转换
    (1封私信/82条消息)如何通俗理解矩阵左乘和右乘的区别?-知乎(zhihu.com) ......
  • Unity 有效降低GPU占用
    此乃经验所得,持续更新中,转载请标明出处1、降低DrawCall可有效降低GPU占用2、通过Application.targetFrameRate主动降低帧率可有效降低GPU占用3、关闭Camera的HDR和MSAA可有效降低GPU的占用,特别是场景中Camera多的时候,效果很明显......
  • Unity框架:JKFrame2.0学习笔记(五)——事件系统(2)
    前言上一篇记录了框架的事件系统的内部构成和实现,这篇记录下事件系统的使用。如何使用新建脚本EventTestusingJKFrame;usingUnityEngine;publicclassEventTest:MonoBehaviour{voidStart(){//添加监听EventSystem.AddEventListener("inp......