【发布时间】:2015-09-09 03:30:08
【问题描述】:
我最近从 SpriteKit 和 Swift 迁移到 Unity,并且开始了一个 Android 项目。我想要的是仅沿 Y 位置上下移动播放器。在 xcode 中,我会简单地这样做:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let action = SKAction.moveToY(location.y, duration: 0.7)
action.timingMode = .EaseInEaseOut
player.runAction(action)
}
}
这种代码会以同样的方式转换为 C# 吗?或者它的工作方式不同。
** 更新 **
这是我尝试使用以下答案之一的代码,但玩家正在消失而不是移动,有什么建议吗?我所追求的是让玩家移动到点击屏幕的位置,但只能在 Y 轴上。谢谢:)
Vector2 touchPosition;
[SerializeField] float speed = 1f;
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// assign new position to where finger was pressed
transform.position = new Vector3 (transform.position.x, Input.GetTouch(i).position.y, transform.position.z);
}
}
}
【问题讨论】: