Hi, iam trying to move gameobject using this script. I want exactly this type of movement where player can touch anywhere on the screen and move gameobject along x axis from that touch position. I don’t want gameobject to move toward touch position or “jump” to touch position. My problem with this code is if player accidentally or purposely use second finger while controlling gameobject, gameobject get odd behavior. I dont want that. I want gameObject to controlled by only one finger.
I have been working on all day on this, i have tried also if(buttonIsPressed) dosomething
If you use Input.mousePosition on a multi-touch tablet, that will contain the average of all touching fingers. Thus when a second finger touches or grazes the screen, the apparent actual touch will jump to the average of the two.
If you want the game to be testable on PC, but also play on multi-touch, it’s handy to use the Application.platform variable to decide which you are on, and then each frame copy either Input.mouseTouch (for PC) or the first element in the Input.touches position over.
Then make your common code operate on that copied position (usually stored in a Vector3).
one question: you want to move the character from touch position or from its current position along x-axis.
second: gameobject should continue move in x-directon till finger kept pressed and stop moving once you release your finger?
try this once
public float REFRESH_TIME = 0.1f;
float refreshDelta = 0.0f;
void NewMouseMove()
{
float smoothingTime = 10.0f; //decrease value to move faster
if (Input.GetMouseButtonDown(0)) {
touchStartPos = Input.mousePosition;
refreshDelta = 0.0f;
}
if (Input.GetMouseButton(0)) {
@Kurt-Dekker has the right of it. But I think you don’t actually need to check the platform; you can just check touchCount, and if that’s > 0, use the first touch, otherwise use the mouse. Like so:
void Update() {
if (Input.touchCount > 0) MoveTo(Input.GetTouch(0).position);
else MoveTo(Input.mousePosition);
}
void MoveTo(Vector2 position) {
// here, do with 'position' whatever you were doing with Input.mousePosition before!
}
So, question. Do you need multitouch at all?
You can disable multitouch with
Input.multiTouchEnabled = false;
Just call this in a starting scene to disable multitouch if you don’t ever need it. I know I use this for a puzzle game where you drag pieces around and needed it to stay with the first touch.
You could also turn it on or off probably if you have different scenes that use multitouch and some that don’t.
I use the same code for mouse and touch. I am using the event system though for dragging pieces around. (OnPointerDown, OnDrag, etc) We had the same issue where a person could tap one finger and then another finger and the puzzle piece jumped across screen. So by using that line of code, Unity only uses the first touch. So now the piece sticks to the first finger touch no matter how many touches the player does.
I don’t see why it wouldn’t work for you. I think all it does is disable multitouch.