- Sort Posts
- 6 replies
- Last post
Re: touch an object
March 29, 2012 - 6:05am #7
Re: touch an object
March 23, 2012 - 10:38am #6
Re: touch an object
March 22, 2012 - 9:21pm #5
Kim, Is it enought to open ImageTarget sample unity project and add this script (Update method of a MonoBehaviour) to one of imagetargets.Or need to add a collider, or another codes to detect the touching ?
I wasn't able to do that with this sample ImageTargetStones and this code:
using UnityEngine; using System.Collections; public class TouchDetection : MonoBehaviour { // Use this for initialization void Start () { Debug.Log("started"); } // Update is called once per frame void Update () { foreach (Touch touch in Input.touches) { Ray ray = Camera.main.ScreenPointToRay(touch.position); Debug.Log("RAY:" + ray.direction); if (touch.phase == TouchPhase.Began) { Debug.Log("touchphase position:" + touch.position); RaycastHit hit = new RaycastHit(); // gameObject.transform.position=new Vector3(gameObject.transform.position.x, // 100, // gameObject.transform.position.z); if (Physics.Raycast(ray, out hit, 1000)) { //never reach here Debug.Log("touched " + hit.transform.name); gameObject.transform.position=new Vector3(gameObject.transform.position.x, 1, gameObject.transform.position.z); } } } } }
Re: touch an object
January 11, 2012 - 10:31pm #3
Re: touch an object
January 11, 2012 - 5:19pm #2
Add a collider to your object and then use a script to capture the touch events, something like this:
foreach (Touch touch in Input.touches) { Ray ray = Camera.main.ScreenPointToRay(touch.position); if (touch.phase == TouchPhase.Began) { RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit, 1000)) { Debug.Log("touched " + hit.transform.name); // Play animation here } } }
- Kim
Yes, you are right, all I need is to add a collider.
Thanks!