- Home /
Mutli-touch in android?
i want to detect if i touch on UI button then our gameobject is not move in the direction of the touch. the problem is when in touch on button, gameobject also move is the direction of touch. look at the image when I touch on the LAUNCH button my pabble which carries ball also move in the direction of launch button on X-axis. but i want it to not move when i touch on any button on screen.
here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paddle : MonoBehaviour
{
//public float playerspeed = 1000;
public float directionalspeed = 50;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float movehorizontal = Input.GetAxis("Horizontal");
transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + movehorizontal, -1.7261f, 1.7261f), gameObject.transform.position.y, gameObject.transform.position.z), directionalspeed * Time.deltaTime);
//mobile controls
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10f));
if (Input.touchCount > 0)
{
transform.position = new Vector3(touch.x, transform.position.y, transform.position.z);
}
}
}
Answer by Zaeran · Sep 08, 2020 at 04:19 PM
I'd look into using the GraphicsRaycast system directly.
First, use the GraphicsRaycast system to do a raycast against UI at the touch position. Then, if there are 0 results, execute your object move code. If there are results, you're hitting the UI
https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.GraphicRaycaster.html
Your answer
Follow this Question
Related Questions
[SOLVED] Touch Controls Not Responsive 1 Answer
two finger touch always detects one finger touch first 1 Answer
Differentiate swipe from tap android 0 Answers
Move Camera with touch (andoid) 1 Answer
check touch position 2 Answers