【问题标题】:object movement as wave in Unity 3dUnity 3d中的物体运动作为波浪
【发布时间】:2013-03-27 15:20:48
【问题描述】:

我统一创建了一个对象

GameObject monsterclone = 
      (GameObject)Instantiate(monsterPrefab, floorPosition, Quaternion.identity);

这个对象应该以波浪的方式从limit1移动到limit2。

然后从limit2回到limit1。

Y 位置和 x 位置必须以特定方式改变。

Vector3 nPos = mfloorPos + new Vector3(2f, 0f, 0f);
Vector3 oPos = mfloorPos  + new Vector3(-2f, 0f, 0f);   

我该怎么做?

【问题讨论】:

  • 上面的代码发生了什么?你有没有尝试创建一个转换而不是一个游戏对象。
  • 这个问题需要澄清一下 - 物体必须进入什么样的波,是 Sin 波、一致的 Saw 波运动还是递减的弹跳运动?图片不够清晰,看不出你想要什么...

标签: unity3d transform monodevelop unityscript


【解决方案1】:

如果不知道更具体的内容,我无法准确编写代码,但我认为这个问题已经被问过,任何这个链接都会帮助你MOVE OBJECT as wave

编辑:

我认为平面向上和向下浮动功能将适用于您将一个点移动到另一个点 示例:

var floatup;
function Start(){
floatup = false;
}
function Update(){
if(floatup)
floatingup();
else if(!floatup)
floatingdown();
}
function floatingup(){
transform.position.y += 0.3 * Time.deltaTime;
yield WaitForSeconds(1);
floatup = false;
}
function floatingdown(){
transform.position.y -= 0.3 * Time.deltaTime;;
yield WaitForSeconds(1);
floatup = true;
}

example taken from

【讨论】:

  • 通过移动 y 位置我们如何制作波浪。我已经搜索并浏览了这些答案。要求你写出有效的答案
  • 它会上下波动。为此,我只能使用乒乓球。但我也想移动 x 个位置.. 我用图像编辑了问题
【解决方案2】:
float amplitudeX = -25.0f;
float amplitudeY = 5.0f;
float omegaX = 0.5f;
float omegaY = 4.0f;
float index;

void Update () {
    index += Time.deltaTime;
    float x = amplitudeX*Mathf.Cos (omegaX*index);      
    float y = Mathf.Abs (amplitudeY*Mathf.Sin (omegaY*index));
    if(transform.position.x > 24){
            transform.eulerAngles = new Vector3(270, -90, 0);
    }
    if(transform.position.x < -24){
            transform.eulerAngles = new Vector3(270, 90, 0);
    }   
    transform.localPosition= new Vector3(x,y,20);
}

【讨论】:

    【解决方案3】:

    如果这是一个持续的波浪并且不依赖于速度,我会使用动画来创建 Position.Y 值的文字波浪曲线(与 Ravindra Shekhawat 解释的原理大致相同。)你可以找到更多关于动画here.

    这是一些您可以关闭的代码(未经测试)。它是在 c# 中的,所以我希望它证明放入 JavaScript 没有问题。

    bool monsterMoving = false;
    
    void Update(){
        //check monster moving to start updating position
        if(monsterMoving == true){
    
            //moving animation controls up and down "wave" movement
            animation.CrossFade('moving'); 
    
            //Lerp changes position
            Transform.Lerp(transform.Position, oPos, Time.deltaTime); 
    
            if (transform.Position == oPos) {
                //We are at destination, stop movement
                monsterMoving = false;
            }
    
        } else {
            // if monster stopped moving, return to idle animation (staying still)
            animation.CrossFade('idle');
        }
    }
    
    // function to send a new position to the monster object
    void MoveTo(Vector3 newPos){
        oPos = newPos;
        monsterMoving = true;
    }
    

    【讨论】:

    • “希望你没有得到我的问题”?不知道我明白你的意思。此解决方案将在编辑之前产生您在问题中描述的效果。 Update() 上两点之间的变换函数和创建上下运动的动画将解决当您的对象从 A 移动到 B 时对象在波浪中上下摆动的问题。它可能不适合您的解决方案您对上图的编辑,但您需要在第一次清楚地解释您的答案,以便我们可以帮助更清楚地回答。
    猜你喜欢
    • 2011-01-09
    • 2017-07-18
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多