加速度计停止动画 运动查询



我有一个名为AccelerometerMovement的脚本,它负责玩家的加速度计控制。播放器只是左右移动,所以我只是Input.acceleration.x组件。

脚本如下:

public class AccelerometerMovement : MonoBehaviour {
    private bool isandroid;
    private float AccelerometerStoreValue;
    private robotController theRobo;
    // Use this for initialization
    void Start () {
        theRobo = FindObjectOfType<robotController> ();

        #if UNITY_ANDROID
        isandroid=true;
        #else
        isandroid=false;
        #endif
    }
    // Update is called once per frame
    void Update () {
        if (isandroid) {
            //android specific code
            Accelerometer();

        } else {
            //any other platform specific code
        }
    }
    void Accelerometer(){
        AccelerometerStoreValue = Input.acceleration.x;
        if (AccelerometerStoreValue > 0.1f) {
            //right
            theRobo.moveRight();
        } else if (AccelerometerStoreValue < -0.1f) {
            //left
            theRobo.moveLeft();
        }
    }
}

正如你在上面看到的根据左和右。.它从另一个脚本调用 moveLeft()moveRight(),该脚本是实际的播放器控制器脚本。

另一个脚本,其中实际函数是:

// after Update()
public void jump(){
        if (grounded) {
            myRigidBody.velocity = new Vector2 (myRigidBody.velocity.x, jumpHeight);
            doubleJump = false;
        }
        if(!doubleJump&&!grounded){
            myRigidBody.velocity = new Vector2 (myRigidBody.velocity.x, jumpHeight);
            doubleJump = true;
        }
    }
    public void moveLeft(){

        myRigidBody.velocity = new Vector2 (-moveSpeed, myRigidBody.velocity.y);
        robotMove = true;
        lastMove = myRigidBody.velocity.x;
        anim.SetFloat ("MoveX", -moveSpeed);
        anim.SetFloat ("LastMoveX", lastMove);
        anim.SetBool ("RobotMoving", robotMove);

    }
    public void moveRight(){

        myRigidBody.velocity = new Vector2 (moveSpeed, myRigidBody.velocity.y);
        robotMove = true;
        lastMove = myRigidBody.velocity.x;
        anim.SetFloat ("MoveX", moveSpeed);
        anim.SetFloat ("LastMoveX", lastMove);
        anim.SetBool ("RobotMoving", robotMove);

    }
    public void stop(){
        robotMove = false;
        anim.SetBool ("RobotMoving", robotMove);
    }

现在,当我检查实际设备上的控件时,控件工作正常,但有一个问题!

问题在于,当玩家开始移动时,移动动画开始,

但当它停止时,空闲动画(或停止动画)不会开始,即使玩家移动动画仍在继续。

现在我无法理解如何解决这个问题。

上面的Stop()函数处于休眠状态。我们需要通过放置一个额外的条件来调用它:

 else if (AccelerometerStoreValue > 0.01f && AccelerometerStoreValue < 0.09f || AccelerometerStoreValue < -0.01f && AccelerometerStoreValue > -0.09f) {
            theRobo.stop ();
        }

这些值将仍然照顾设备,并将空闲动画设置为工作!如果在函数中,请将上面的代码放在第二个其他之后Accelerometer().

最新更新