x


Touch movement not working

I am trying to move a gameObject by touch.

    #pragma strict

var player : GameObject;
var moveToPos : Vector3;
var touchPos : Vector3;
var myTransform : Transform;
var moveSpeed : int;

function start ()
{
   myTransform = transform;
}

function Update ()
{
   touchPos = Input.mousePosition;

   player.transform.position.z = 5;

   if (Input.GetMouseButton(0)) {

   moveToPos = Vector3 (touchPos.x , myTransform.position.y , 5);   

   myTransform.Translate(moveToPos * moveSpeed * Time.deltaTime);

   Debug.Log(touchPos);

}
}

But no matter where I touch the object zooms to the right. I have asked someone with more experienced than me in unity and he has no idea why it isn't working. I also noticed that i had to set all the variable's values manually in the inspector.

more ▼

asked Mar 08 '12 at 08:52 PM

MithosAnnar gravatar image

MithosAnnar
299 22 36 40

I imported meshes from silo.

Mar 09 '12 at 03:07 PM MithosAnnar
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Input.mousePosition returns ScreenCoordinates in pixels. Those are always > 0. So the x value of your velocity vector (the one you use to translate) will always be > 0 hence you move to the right.

What you have to do is convert the ScreenCoordinates to WorldCoordinates. You can do that using Camera.ScreenToWorldPoint.

more ▼

answered Mar 09 '12 at 11:04 AM

StephanK gravatar image

StephanK
6k 39 53 93

I have done a lot of editing and have come up with this:


var moveSpeed : int = 10;
var mousePos : Vector3;

function Update () {

   transform.position.z = 2;

   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   
if(Input.GetMouseButtonDown(0)) {

   transform.Translate (Vector3 (10, 9, 2));

}
}

However when i click the object moves randomly around the screen. I have consistently encountered this problem when trying to translate my object to a Vector3.

Mar 14 '12 at 01:54 PM MithosAnnar

It seems like given that code it would translate the object (10,9,2) no matter where you clicked, now wouldn't it?

Mar 14 '12 at 03:11 PM mpavlinsky

@mpavlinsky, No. It doesn't even translate there. when I tell it to move to (10, 9, 2) it moves where ever it wants to. And I have no idea why.

Mar 14 '12 at 03:33 PM MithosAnnar

Is it a problem with transform.Translate?

I can't translate to anywhere I specify. Let alone my mouse position!

Mar 14 '12 at 03:35 PM MithosAnnar

Translate moves the object by whatever you pass in, not to it. This is my understanding from the script reference at least, I have never actually used this function.

You probably want transform.position = whatever;

Mar 14 '12 at 03:47 PM mpavlinsky
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1362
x576
x254

asked: Mar 08 '12 at 08:52 PM

Seen: 614 times

Last Updated: Mar 14 '12 at 05:11 PM