- Home /
Get the position of the first finger that touched the collider?
Is there a way I can get the touch position of only the finger that FIRST touched a certain collider?
For example, When I touch a collider, it will get the position of that touch. And when I touch the same collider again with a different finger AND I haven't let go of my first touch, it will just ignore the 2nd touch, and I should only have the position of the first initial touch.
EDIT: Only the first touch that hits the collider, not the actual first touch on the whole screen. Input.GetTouch(0) kind of gets the actual very first touch. So if the player hits the collider on the second touch, then it's (1), but it's the FIRST touch on that certain collider, even though in actuality, it's the second touch.
I'm not sure how to do this, I'm still kind of new to touches and stuff.
The current code I use returns the position of the first and second touch. So when I touch something, it will get that position, and when I touch it again, the position will change, as if it's combining both positions.
This is just an assumption though, I'm not sure if that's really the problem.
Here it is just in case: It's a joystick I use in moving my character. Whenever I just touch it once, it will do its work properly, But when I touch it again with another finger, my character speeds up, and there's no script that when I touch it twice, it will speed up. So I assumed it combining the position of the first and second, thus making my player move faster when I'm touching the same collider twice.
And yeah, it's not using anything that has to do with fingerIDs, because I'm still not sure how to use it in this situation.
void Update () {
//Handles touches
for(int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
Ray ray = GUICamera.ScreenPointToRay(Input.touches[i].position);
touchPosition = Input.touches[i].position;
if(Physics.Raycast(ray, out hit) && hit.collider.tag == "joystickTag")
{
//the position of where the player touched
touchPosition = GUICamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 72));
//where you want to be (according to MoveTowards)
Vector3 desiredPosition = touchPosition;
//your clamp is relative to this position
Vector3 origin = centerPos.position;
//this vector points from 'origin' to 'desiredPosition'
Vector3 diff = desiredPosition - origin;
//clamp the difference, then add it to the origin
transform.position = origin + Vector3.ClampMagnitude(diff, maxRange);
tempDelta = new Vector3(transform.position.x - oPos.x, -72, transform.position.z - oPos.z).normalized;
delta = Vector3.Lerp(delta, tempDelta, Time.deltaTime * 8.0F);
}
}
//This moves my player
player.transform.Translate(((Vector3.forward * delta.z) * playerSpeed) * Time.deltaTime);
player.transform.Translate(((Vector3.right * delta.x) * playerSpeed) * Time.deltaTime);
}
Again though, I'm still not quite sure why it's going fast.
Thanks!
EDIT: Still need some help. :/
Sorry I forgot to mention that I had another button that the player needs to press as well. So what I actually wanted was the first touch THAT hits the collider, not the actual first touch on the entire screen. Forgot to mention, sorry.
Alright lol ... was taking a stab in the dark with the first thing that came to $$anonymous$$d in case it might help xD
@tanoshimi I'm not sure how to do that in my situation though, I'm new to fingerIDs and Google and Unity Documentations doesn't help. I've actually tried and searched a lot about this on how to solve my problem, but none worked properly for me so I just asked myself.
Answer by tanoshimi · Jul 19, 2014 at 08:23 AM
It's hard to follow your scenario, but for any multitouch applications I tend to maintain an internal array/dict/list/whatever that records fingerid, object touched etc. and then process all game logic based on that rather directly trying to apply logic to Input.touches. You might want to give that a try.
Your answer
Follow this Question
Related Questions
Move an object to Input.Touch location 0 Answers
Issues with Bullet Ricochet 1 Answer
Touch buttons for step movememt 3 Answers
Touch Crosshair problem. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers