繁体   English   中英

通过使用 android sdk 触摸 Unity3d 来拖动对象

[英]Dragging an object by touching Unity3d with android sdk

我试图通过触摸和拖动来移动和反对。 我正在我的三星 Galaxy SIII 上测试它。 我使用了以下代码。 出于某种原因,它比我的手指移动得更快。 它应该总是在我的手指下。 怎么了? (注意:我还没有完成“仅当您触摸它时才移动对象”部分,所以现在它会移动到我触摸的地方)。

#pragma strict

var speed : float = 1;

function Start () {

}

function Update () { 
    if (Input.touchCount > 0 && Input.GetTouch(0).phase ==     TouchPhase.Moved) {

    // Get movement of the finger since last frame
    var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;


    // Move object across XY plane
    transform.position = Vector2.Lerp(transform.position,
                                            touchDeltaPosition, 
                                            Time.deltaTime*speed);
}
}

这就是我使用的,它对你来说可能是一个更好的选择,它基于相机,你的 Vector2.Lerp 无法正常工作的原因我认为是因为你的时间变量,你可以参考这个Lerp和调整你的“t”变量直到它对你有好处,或者你可以试试这个,这就是我使用的,我也从 x 中减去并添加到 y 所以我的手指不在图形上,祝你好运:)

#pragma strict

var speed : float = 1;
var distance : float = 5;
function Start () {

}

function Update () { 
    if (Input.touchCount > 0 && Input.GetTouch(0).phase ==     TouchPhase.Moved) {

    // Get movement of the finger since last frame
    var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;

    var touchmove : Vector3 = Vector3(touchDeltaPosition.x, touchDeltaPosition.y, distance);


    // Move object across XY plane
    transform.position = Camera.main.ScreenToWorldPoint(touchmove);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM