简体   繁体   English

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

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

I am trying to move and object by touching it and dragging.我试图通过触摸和拖动来移动和反对。 I am testing it on my Samsung Galaxy SIII.我正在我的三星 Galaxy SIII 上测试它。 I have used the following code.我使用了以下代码。 For some reason it moves faster than my finger.出于某种原因,它比我的手指移动得更快。 It should always be beneath my finger.它应该总是在我的手指下。 What is wrong?怎么了? (note: I haven't done the "move object only if you touch onto it" part, so right now it moves where ever I touch). (注意:我还没有完成“仅当您触摸它时才移动对象”部分,所以现在它会移动到我触摸的地方)。

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

this is what i use, it may be a better option for you, it is based on the camera, the reason that your Vector2.Lerp is not working correctly i think is because of your time variable in it you could refer to this Lerp and tweak your 't' variable until it is good for you, or you can try this, this is what i use, also i subtract from x and add to y so my finger isnt over the graphic, best of luck :)这就是我使用的,它对你来说可能是一个更好的选择,它基于相机,你的 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