如何在Unity3D中临时在z轴上旋转对象?

时间:2019-01-06 05:34:43

标签: c# 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();
}

1 个答案:

答案 0 :(得分:1)

我同意“ 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());
}