当前位置: 首页 > news >正文

Unity模拟谐波运动

Unity模拟谐波运动
先上图
在这里插入图片描述

上代码, 很简单, 就一个颜色渐变和不同的旋转速度

using UnityEngine;public class Mgr : MonoBehaviour
{const int count = 56;[SerializeField]Transform prefab;Transform[] balls = new Transform[count];Material[] materials = new Material[count];int[] currIndexs = new int[count];Color[] colors = new Color[6] { new Color(1, 0, 0), new Color(1, 1, 0), new Color(0, 1, 0), new Color(0, 1, 1), new Color(0, 0, 1), new Color(1, 0, 1) };float scale = 0;float basicSpeed = 50;float speedDifference = 2;void Start(){for (int i = 0; i < count; i++){Vector3 pos = new Vector3(-3 - i * 0.3f, 0, -i);Transform ball = Instantiate(prefab, pos, Quaternion.identity);Material material = ball.GetComponent<Renderer>().material;balls[i] = ball;materials[i] = material;currIndexs[i] = 0;materials[i].color = colors[0];RefreshScale(i);for (int j = 0; j < i; j++){RefreshColor(j);}}}void Update(){for (int i = 0; i < count; i++){RefreshColor(i);Rotate(i);}if (Input.GetKeyDown(KeyCode.Space)){scale = Random.Range(-0.05f, 0.05f);basicSpeed = Random.Range(-200, 200);speedDifference = Random.Range(-20f, 20f);for (int i = 0; i < count; i++){RefreshScale(i);}}}void RefreshScale(int i){balls[i].localScale = Vector3.one * (1 + i * scale);}void RefreshColor(int i){int currIndex = currIndexs[i];int nextIndex = (currIndex + 1) % 6;Color dir = colors[nextIndex] - colors[currIndex];Color currColor = ColorAdd(materials[i].color, dir * Time.deltaTime);materials[i].color = currColor;if (currColor == colors[nextIndex]){currIndexs[i] = nextIndex;}}Color ColorAdd(Color color1, Color color2){float r = Mathf.Clamp01(color1.r + color2.r);float g = Mathf.Clamp01(color1.g + color2.g);float b = Mathf.Clamp01(color1.b + color2.b);return new Color(r, g, b);}void Rotate(int i){balls[i].RotateAround(Vector3.zero, Vector3.forward, (i * speedDifference + basicSpeed) * Time.deltaTime);}
}

一想到AI可以生成代码, 我还在手敲, 觉得好虚无啊…

http://www.dtcms.com/a/414779.html

相关文章:

  • Overleaf编译超时,超出免费计划编译时限(已解决)
  • MySQL 主主复制 + keepalived + HAProxy
  • ARM Synchronization Primitives
  • 好网站建设公司哪家好网站建设首选九零后网络
  • 负载均衡式的在线OJ项目编写(四)
  • Redis 解锁:C++ 实战深度探索 Set 数据类型
  • Nginx 核心安全配置总结
  • xbatis基于 mybatis 的 ORM 框架
  • Spring Gateway动态路由实现方案
  • 网站在线提交询盘系统 能直接发到邮箱深圳市工业设计行业协会
  • Python编程练习:7个经典题目详解
  • JCR分区,中科院分区,CCF分区的含义与区别
  • 二型最大似然(Type II Maximum Likelihood):概述与核心概念
  • 娄底网站建设开发自媒体素材视频网站
  • 云浮源峰网站建设工作室地址门户网站建设相关需求
  • 鸿蒙父组件处理子组件的点击事件
  • DDPM原理解析
  • 白盒密码:守护不可信环境中的密钥安全
  • 创建一个网站买卖WordPress外链方法
  • 使用 Go SDK 玩转 Docker:从容器到多架构构建
  • 原生微信小程序开发基础知识总结架构逻辑
  • 树莓派实现的自动垃圾(纸团)回收机器人
  • 【ROS2学习笔记】节点篇:节点概述
  • Java面试宝典:网络协议与Netty二
  • 自然语言处理(01)
  • 如何利用模板 + 继承的方式,形成动态多态的效果呢?
  • UE_ContrlRig
  • 江苏省城乡住房建设厅网站网站建设及维护流程图
  • ubuntu18.04安装五笔字型的方法
  • 上市公司网站建设要求成都丁香人才网官网专区