Cube翻滚前进
方法一:给Cube添加rigidbody,然后代码rigidbody.AddTorque (10, 0, 0);将使Cube沿X轴翻滚
方法二:Cube没有 添加rigidbody,利用以下代码计算翻滚
// Reposting this non-working answer because it was unreadable when posted as a comment to the previous answer
var cubeSize : float = 1;
var cubeSpeed : float = 80;
private var totalRotation : float = 0; // determines if we're past the 90 degrees
private var startHeight : float = 0; // for correcting height errors
// next two vars are for determining the corner
// to rotate over
private var fwdWeight : float = 0.5;
private var upWeight : float = -0.5;
// Use this for initialization
function Start () {
startHeight = transform.position.y;
}
// Update is called once per frame
function Update () {
DoRoll(2);
}
function DoRoll (delay: float) {
var spinAmount : float = Time.deltaTime * cubeSpeed;
var t : float;
var pos : Vector3;
// we rotate around one of the edges of the cube (the stationary one of course)
transform.RotateAround(transform.position + (fwdWeight * transform.forward + upWeight * transform.up) * cubeSize, Vector3.right, spinAmount);
// add to amount of spin in this update the total rotation
totalRotation += spinAmount;
// check if we have to move to the next edge
if (totalRotation >= 90) {
// we move to next corner as pivot point
totalRotation -= 90;
t = fwdWeight;
fwdWeight = -upWeight;
upWeight = t;
// make sure height stays correct
pos = transform.position;
pos.y = startHeight;
transform.position = pos;
print ("At rotation " + totalRotation);
yield WaitForSeconds(delay);
}
}
var cubeSize : float = 1;
var cubeSpeed : float = 80;
private var totalRotation : float = 0; // determines if we're past the 90 degrees
private var startHeight : float = 0; // for correcting height errors
// next two vars are for determining the corner
// to rotate over
private var fwdWeight : float = 0.5;
private var upWeight : float = -0.5;
// Use this for initialization
function Start () {
startHeight = transform.position.y;
}
// Update is called once per frame
function Update () {
DoRoll(2);
}
function DoRoll (delay: float) {
var spinAmount : float = Time.deltaTime * cubeSpeed;
var t : float;
var pos : Vector3;
// we rotate around one of the edges of the cube (the stationary one of course)
transform.RotateAround(transform.position + (fwdWeight * transform.forward + upWeight * transform.up) * cubeSize, Vector3.right, spinAmount);
// add to amount of spin in this update the total rotation
totalRotation += spinAmount;
// check if we have to move to the next edge
if (totalRotation >= 90) {
// we move to next corner as pivot point
totalRotation -= 90;
t = fwdWeight;
fwdWeight = -upWeight;
upWeight = t;
// make sure height stays correct
pos = transform.position;
pos.y = startHeight;
transform.position = pos;
print ("At rotation " + totalRotation);
yield WaitForSeconds(delay);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架