- Home /
Can't make cube follow the finger.
Hello, I've just started scripting in unity and I'm a total noob. I need my cube follow a finger but not going up on y axis, that's all. Always follow a finger, but my version of code isn't working. My cube always going back to the start position. I searched for the info in internet, but after 2 hours of searching didn't find anything. All I found is Camera.main.ScreenToWorldPoint and I thought that the cube always returns to the start position because my touch is considered on the start screen and I should make it "WorldPoint" but nope, that didn't work. Help... Please.
void Update()
{
if (Input.touchCount > 0)
{
Vector3 touch = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
if (Input.GetTouch(0).phase == TouchPhase.Stationary || Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, 0, touch.z));
transform.position = touchPosition;
}
}
}
}
Answer by highpockets · Apr 18, 2019 at 12:07 PM
The touch.z needs to be the distance away from the camera that you want your object. If your camera is aligned/facing along the z axis. Get the z distance:
float zDistance = cube.transform.position.z - camera.transform.position.z;
Then:
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, 0, zDistance));
transform.position = touchPosition;
You could also just get the Vector3.Distance(fromPosition, toPosition); which returns a float, but be careful of this in 3D perspective space because if an object is far out to the right or left, the object will use the camera as a pivot, essentially rotating around it. That’s why I would opt for getting the distance on the specific axis to eli$$anonymous$$ate the pivoting effect if the camera is stationary or at least doesn’t rotate
I got the idea, but for some reason it goes down left now, always. It was right up and back before. I just don't understand why :/
Oh, nope, Now I've got another problem but it worked. You solved my problem, thank you!! Code that I have now: void Update() { if (Input.touchCount > 0) {
Vector3 touch = (Input.GetTouch(0).position);
if (Input.GetTouch(0).phase == TouchPhase.Stationary || Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved)
{
float zDistance = cube.transform.position.z - camera.transform.position.z;
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, 0, zDistance));
transform.position = touchPosition;
}
}
}
Ok, I’ll convert it to an answer then, cheers
Answer by qobion · Apr 18, 2019 at 10:39 AM
You are converting screen point to world 2 times. Remove first
Vector3 touch = Input.GetTouch(0).position;
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch);
touchPosition.y = 0;
transform.position = touchPosition;
Also touch position is vector2(x,y) So to convert it to point you should use all coordinates
It's not working too. Still, cube just moving up back and right and with very high speed.
Your answer
Follow this Question
Related Questions
How do I make a 3D character move and rotate (turn) the same direction they move? c# 0 Answers
How do I move an object with my finger?[C#] 1 Answer
Any ideas why my buttons work in the editor but not on mobile? 0 Answers
File size increases on different devices 1 Answer
Slowly move a GameObject on 1 axis, then destroy it. 1 Answer