- Home /
 
conversion of script guitexture to cube or other primitive (iphone)
hello folks! i have this script
 function Update () {
     
  for (var touch : Touch in Input.touches)
  {
   if (guiTexture.HitTest (touch.position))
   {
     // we are now in the guitexture
     
     Application.LoadLevel(toLoad);
    } 
 
               but i want to use something similar on a cube or sphere or such primitive so that i can detect touch on it, some help would be appreciated. Thank you
if you din't got it , i am referring touch to iphone/android touch
Answer by mohanrao164 · Sep 02, 2011 at 10:49 AM
In the standard assets the "DragRigidbody script" is there it will help u to detect the touch of the sphere or cube.
It is for the onmouseclick . A little changes have to make for iphone/andriod so it will work fine.
i have got the touch in my device.if any query let me come to know.
i don't want touch to happen when camera moves , i want touch to happen strictly when i touch it on my android screen
will u be little clear so i can understand.u want wheather the object is touched or not or u want the object touch and moved.
ok i just want something to happen when i touch the cube on my iphone/android
k i will give u the code in which i touched the sphere and drag anywhere in the scene or device. the file is in c# script. so i hope it would be helpful.
Answer by mohanrao164 · Sep 05, 2011 at 05:22 AM
This code will help u and it is working for me.
using UnityEngine;
using System.Collections;
public class DragRigidbody : MonoBehaviour
{
 public float spring = 50.0f;
 public float damper = 5.0f;
 public float drag = 10.0f;
 public float angularDrag = 5.0f;
 public float distance = 0.2f;
 
 public  int nameOfConnectedBody;
 public  bool  attachToCenterOfMass = false;
 public  bool isOrbSelected;
 
 private  SpringJoint springJoint;
 private  Touch touch;
 public   string actname;
 
 void Update ()
 {
     
     if (Input.touchCount == 0) 
     {
         return;
     }
     Camera mainCamera = FindCamera ();
     RaycastHit hit = new RaycastHit ();
     transform.position = new Vector3 ( transform.position.x, 2.0f, transform.position.z );
     
 
     touch = Input.GetTouch (0);
     
     if (touch.phase == TouchPhase.Began) 
     {
         if(!Physics.SphereCast(mainCamera.ScreenPointToRay (touch.position),3.0f,out hit,300.0f))
         return;
         if (!hit.rigidbody || hit.rigidbody.isKinematic)
         return;
         if (!springJoint) 
         {
             GameObject go = new GameObject ("Rigidbody dragger");
             springJoint = (SpringJoint)go.AddComponent ("SpringJoint");
             go.rigidbody.isKinematic = true;
         }
         
         springJoint.transform.position = hit.point;
         
         if (attachToCenterOfMass) 
         {
             Vector3 anchor = transform.TransformDirection (hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
             anchor = springJoint.transform.InverseTransformPoint (anchor);
             springJoint.anchor = anchor;
         } 
         else 
         {
             springJoint.anchor = Vector3.zero;
         }
         
         springJoint.spring = spring;
         springJoint.damper = damper;
         springJoint.maxDistance = distance;
         springJoint.connectedBody = hit.rigidbody;
             
         StartCoroutine ("DragObject", hit.distance);
         isOrbSelected = true;
     }
 }
 
 IEnumerator DragObject (float distance)
 {
     if (touch.phase == TouchPhase.Began)
     {
         float oldDrag = springJoint.connectedBody.drag;
         float oldAngularDrag = springJoint.connectedBody.angularDrag;
         springJoint.connectedBody.drag = drag;
         springJoint.connectedBody.angularDrag = angularDrag;
         Camera mainCamera = FindCamera ();
         
         while (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
         {
             transform.position = new Vector3 ( transform.position.x, 2.0f, transform.position.z );
             nameOfConnectedBody =  springJoint.connectedBody.gameObject.GetInstanceID();
             actname = springJoint.connectedBody.gameObject.name;
             Ray ray = mainCamera.ScreenPointToRay (touch.position);
             springJoint.transform.position = ray.GetPoint (distance);
             yield return 0;
         }
         
         if (springJoint.connectedBody) 
         {
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
     
         isOrbSelected = false;
         nameOfConnectedBody = 0;
         actname="";
         
         
     }
     
 }
 Camera FindCamera ()
 {
     if (camera)
         return camera;
     else
         return Camera.main;
 }
 
               }
Your answer