# supposedly simple transformation

hey guys, this is really weird and doesn’t make any sense to me. Hopefully someone here can help me

I am trying to move something around essentially by dragging it around the screen. i’m using the following piece of code based on the free to the community pong project I found on the forums (by the way, thanks a lot to the author of that, it’s helped me loads!)

var p2 : Vector3 = cam.ScreenToWorldPoint (Vector3 (Input.mousePosition.x,Input.mousePosition.y,15));
//player1.position.y = Mathf.SmoothDamp(player1.position.y, p2.y, touchVelocity, touchSpeed);
player1.position.z = Mathf.SmoothDamp(player1.position.z, p2.z, touchVelocity, touchSpeed);
//player1.position = Mathf.SmoothDamp(player1.position, p2, touchVelocity, touchSpeed);

The third position doesn’t work. Doesn’t matter right now.

The problem is that I can either have position z or position y move using the mouse but not both at the same time.

When I use both the top two transformation positions, the position just translates from bottom right to top left.

Any ideas or solutions?

dreamblur, i know that line is wrong. i only really put it there as evidence that i’d been trying to find my own solution to the problem, even if it was a rubbish solution

Mathf.SmoothDamp only works in one dimension variables, like position.y and position.z. You need to call it for y and z separatelly like you did in the two first lines. If the object you’re trying to drag is going to the wrong place, the problem may be the ScreenToWorldPoint function. In a top-down game like this, the camera must be set at some height, looking directly to the plane where the game runs. Since the mouse position is a bidimensional thing, there’s no way to convert it to 3D unless you supply a 3rd coordinate. In such a game, it must be the height of the camera position minus the height of the character position. Assuming you really want your game to run in the ZY plane (see note below), the 3rd parameter should be set this way:

var h = cam.transform.position.x - player1.position.x;
var p2 = cam.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,h));

If you set the 3rd parameter to other value, ScreenToWorldPoint will give wrong coordinates (as @Dreamblur pointed out), and the object will go to God-knows-where. If this still seems to do the wrong thing, try the following instead of both SmoothDamp:

player1.position = p2;

If the player follows the mouse pointer, then the problem must be in SmoothDamp (it’s a somewhat complex function to beginners).

NOTE: By the way, are you sure you’re going to use the ZY plane? usually XZ is used, with the camera set at some fixed Y coordinate above the ground; some things in unity (like terrain and character controllers) could not be used in such a game if it’s not XZ based.