对三角函数进行实际操作,需要对木马移动进行平滑插值 木马起伏采用的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;
}
}
标签:Mathf,horseNodes,float,pos,旋转,owenRotateCyc,Unity,木马,new
From: https://www.cnblogs.com/itcod/p/17342781.html