- Home /
How do I detect touch on another object and check by tag/name?
I have a UI button set up in the canvas named 'Top'. The script I want to edit is on the vehicle that I want to move when the button is being touched.
.
So I need to do some form of: public void touch.down("Top") { transform.position(); }
.
More Info: I have tried many different methods for this to work. I have even tried using getcomponent to read a variable on the button script (although this would be running every tick on mobile) but that failed to read the script with a namespace issue.
.
How would I do this please?
Did you try the solution from here? And did you try the easiest method of simply setting the OnClick() event listener in the inspector? If so, what did go wrong?
Answer by julio-hb · Jan 14, 2019 at 11:12 PM
If im understanding correctly:
you want to move a vehicle when you're pushing a UI button?
if so, try this:
class used for getting input, and moving the vehicle
public class test : MonoBehaviour {
public Transform vehicle;
public float speed = 10;
public void Move()
{
vehicle.Translate(vehicle.forward * Time.deltaTime * speed);
}
}
when simply create a button, and call the function when the button is pressed.
alternatively, you could the the Ipointer-interfaces as such: this method, you don't need to assign a event on the button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class test : MonoBehaviour,IPointerDownHandler,IPointerUpHandler {
public Transform vehicle;
public float speed = 10;
//users pressing the button
public void OnPointerDown(PointerEventData eventData)
{
vehicle.Translate(vehicle.forward * Time.deltaTime * speed);
}
//user releases the button
public void OnPointerUp(PointerEventData eventData)
{
throw new System.NotImplementedException();
}
}
Your answer
Follow this Question
Related Questions
For mobile games, how could you divide the screen into three buttons vertically? 1 Answer
Shoot cannonballs at the push of a button 1 Answer
Making one directional virtual joystick OR smoothly transitioning between buttons 0 Answers
How do I make a "move pad" on the screen? (Mobile) 1 Answer
How to set mobile control and scale in size in android setting? 1 Answer