首页 > 解决方案 > 使用动画在 x 轴上移动对象

问题描述

我想用动画在 x 轴上移动游戏对象。我写了下面的代码,但它只有在我清除 input.getkeydown 条件时才有效。如果按下键,有没有办法做到这一点?谢谢

void Update()
 {

     if (Input.GetKeyDown(KeyCode.Space))
     {
         float delta = speed * Time.deltaTime;
         Vector3 currentPosition = gameObject.transform.position;
         Vector3 nextPosition = Vector3.MoveTowards(currentPosition, new Vector3(center, transform.position.y, transform.position.z), delta);

         // Move the object to the next position
         Debug.Log("delta" + delta);
         Debug.Log("nextposition x" + nextPosition.x.ToString());
         gameObject.transform.position = nextPosition;
     }
}

标签: unity3danimation

解决方案


您可以简单地以统一的方式逐帧录制动画,然后在清醒时播放动画或使用脚本使用触​​发方法


推荐阅读