沿着Y pos移动玩家

时间:2015-08-27 14:53:04

标签: c# android unity3d

我最近从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);

                }
            }    
        }

3 个答案:

答案 0 :(得分:1)

您可以通过从该对象的脚本调用transform.Translate(),或者通过获取对另一个对象的转换并在其上调用Translate()来移动场景中的任何对象。

根据手指移动移动当前脚本的场景对象

Vector2 touchDeltaPosition;
[SerializeField] float speed = 1f;

void Update() {

    for (var i = 0; i < Input.touchCount; i++) {

        if (Input.GetTouch(i).phase == TouchPhase.Moved) {

            // get new finger position
            touchDeltaPosition = Input.GetTouch(i).deltaPosition;

            // move scene object along y axis
            transform.Translate(0f, -touchDeltaPosition.y * speed, 0f);

        }

    }    

}

将当前脚本的场景对象移动到手指按压位置

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);

        }

    }    

}

答案 1 :(得分:0)

public float speed = 5.0f;

public void Update()
{
    Vector3 moveDelta = new Vector3(0f, Input.GetAxis("Vertical"), 0f);
    transform.Translate(moveDelta * speed * Time.deltatime)
}

答案 2 :(得分:0)

对于任何需要了解的人来说,这就是我的工作方式:

公共GameObject thingToMove;

public float smooth = 2;

private Vector3 _endPosition;

private Vector3 _startPosition;

private void Awake()
{
    _startPosition = thingToMove.transform.position;
}

private void Update()
{
    if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
    {
        _endPosition = HandleTouchInput();
    }
    else
    {
        _endPosition = HandleMouseInput();
    }

    thingToMove.transform.position = Vector3.Lerp(thingToMove.transform.position, new Vector3(transform.position.x, _endPosition.y, 0), Time.deltaTime * smooth);
}

private Vector3 HandleTouchInput()
{
    for (var i = 0; i < Input.touchCount; i++) 
    {
        if (Input.GetTouch(i).phase == TouchPhase.Began) 
        {
            var screenPosition = Input.GetTouch(i).position;
            _startPosition = Camera.main.ScreenToWorldPoint(screenPosition);
        }
    }

    return _startPosition;
}

private Vector3 HandleMouseInput()
{
    if(Input.GetMouseButtonDown(0))
    {
        var screenPosition = Input.mousePosition;
        _startPosition = Camera.main.ScreenToWorldPoint(screenPosition);
    }

    return _startPosition;
}

使用.Lerp将对象平滑移动到触摸位置。更多信息: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html