- Home /
Working with Touch screens...
I got these instantiated objects which are clones. Currently I have them setup to react when clicked using 'OnMouseDown()' function. How can I get the same effect but on a iPad and iPhone.
function OnMouseDown () {
animation.Play("TruckerprivateRun");
ScoreController.Points += tap + 100;
audio.Play();
}
Continued...
I found a Raycast script that could work. I made it to where when ever an object that has a the Tag "Target" is touched it activates the function "OnMouseDown." Is this possible or are their changes I should make?
function Update () {
var touch = Input.GetTouch[0];
if (touch.phase == TouchPhase.Began) {
var ray = Camera.main.ScreenPointToRay (touch.position);
var hit: RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (other.gameObject.CompareTag ("Target")) {
gameObject.SendMessage ("OnMouseDown");
}
}
}
}
}
Answer by dannyskim · Dec 10, 2011 at 11:31 PM
You can use the Input.touches:
http://unity3d.com/support/documentation/ScriptReference/Input-touches.html
or, you can use Input.GetMouseButton(int), Input.GetMouseButtonDown(int), etc:
http://unity3d.com/support/documentation/ScriptReference/Input.GetMouseButton.html
Of course with the second option, this only detects one finger as far as I'm aware. There may be a way to use Input.GetMouseButton to detect more than one touch, but I'm not aware of how to do that myself.
So, if by putting one of these on a gameObject it will only trigger if I hit that gameObject? Will it trigger if I touch anywhere?
I tried using Raycast to send$$anonymous$$essage("On$$anonymous$$ouseDown") and that didn't work at all. Please explain, I've been only using On$$anonymous$$ouseDown and OnTriggerEnter type of interaction. :/
No, Input.Get$$anonymous$$ouseButton detects a mouse down anywhere on the screen. It's up to you to define what gets detected through further RayCast logic.
Take a look at the documentation for Physics.Raycast:
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
There's an example further down the page on that link that shows you how to use a raycast with mouseposition.
hows this script I added to my question? would that work?