簡體   English   中英

如何在Unity3D中臨時在z軸上旋轉對象?

[英]How would I temporarily rotate an object on the z-axis in Unity3D?

我試圖在Unity3D中為角色制作打孔動畫,我需要在z軸上旋轉手臂,但要使其具有動畫效果,我到處都在尋找解決方案。 似乎沒有任何作用。 這是我到目前為止的內容:

PlayerMotor:

public void Punch() {
    arm.transform.Rotate(0, Time.deltaTime, 0);
    arm.transform.position= new Vector3(arm.position.x, arm.position.y, arm.position.z + .01f);
}

public void PunchReturn() {
    arm.transform.Rotate(0, -Time.deltaTime, 0);
    arm.transform.position = new Vector3(arm.position.x, arm.position.y, arm.position.z - .01f);
}

的PlayerController:

if (Input.GetMouseButtonDown(0)) {
    // Does punching animation
    Debug.Log("punching");
    for (int i = 0; i < 50; i++) motor.Punch();
    for (int i = 0; i < 50; i++) motor.PunchReturn();
}

我同意“ trollingchar”,協程或動畫師是要走的路。 如果您想使用協程:

使此方法:

// this runs "in parallel" to the rest of your code.
// the yield statements will not freeze your app.
IEnumerator AnimationCoroutine(){
    for (int i = 0; i < 50; i++) {
        motor.Punch(); // rotate a little bit
        yield return null; // waits for one frame
    }
    for (int i = 0; i < 50; i++) {
        motor.PunchReturn(); // rotate a little bit
        yield return null; // waits for one frame
    }
}

並從控制器調用它

if (Input.GetMouseButtonDown(0)) {
    // Does punching animation
    Debug.Log("punching");
    StartCoroutine(AnimationCoroutine());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM