物体运动如Unity 3d中的波浪

时间:2013-04-05 09:14:53

标签: unity3d transform monodevelop unityscript

我创造了一个统一的对象

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);   

我该怎么做?

enter image description here

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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);
}

答案 2 :(得分:0)

如果这是一个伴随波并且不依赖于速度我将使用动画来创建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;
}