- Home /
Move gameObject with swipe in 3d space ?
Hello !! I tried using the default unity Input.GetTouch(). I used Vector3 instead of Vector2. Unity link. But it dosen't get swiped in the z-direction. Please help
// Moves object according to finger movement on the screen
var speed : float = 0.1;
function Update () {
if (Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
var touchDeltaPosition:Vector3 = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (touchDeltaPosition.x * speed,
touchDeltaPosition.y * speed,touchDeltaPosition.z * speed);
}
}
Answer by jotapeh_ · Jun 26, 2014 at 05:29 PM
Touch.deltaPosition is a Vector2, which means there is no Z property on it. You are implicitly casting it to a Vector3 which means Z will always be 0.
If you want it to move in the Z direction, you're going to have to make some decisions about what that actually means, since a 2D swipe across a screen does not have a direct equivalent in 3D.
hey jotapeh !! I want to move the football in the direction of swipe and also add force to it proportional to the swipe length. It needs to travel in the z-direction also. I'd added rigidbody.addForce(touchDeltaPosition x,touchDeltaPosition y,touchDeltaPosition*z)
It doesn't work. Can you suggest some code for this ?
I'd suggest using a raycast from the touch point into the scene and move your object based on the raycast hit point.
Your answer
Follow this Question
Related Questions
Android Drag By Steps 0 Answers
How add click area? 1 Answer
Unity2D iOS Dragable items by touch 0 Answers