x


touch game objects on ios

Hello,

I ported a pc project to the iPad today and I noticed my script for opening doors wasn't working anymore, this script gets triggered when the player clicks the door game object. I figured I needed to use ray cast for this and yes I can open my door now using this script

 var studioBlock : Transform; // blocker behind the door to stop you entering a room
private var doorObject : GameObject; // the door gameobject (has a collider)
var doorName = "stdr"; // name of the door gameobject
private var hit : RaycastHit;
private var ray : Ray;
private var touchpos : Touch;

function Start(){

doorObject = GameObject.Find(doorName);

}

function FixedUpdate (){
    if (Input.touchCount > 0) {
       ray = Camera.main.ScreenPointToRay(touchpos.position); //I can't get touch.position to work
       Debug.DrawLine(ray.origin,ray.direction * 1000);
       if (Physics.Raycast(ray.origin, ray.direction * 1000,hit)){
         if (hit.collider.tag == "stdr"){
          doorObject.transform.animation.Play();// plays the door animation
          studioBlock.position.y = -100;// removes the room blocker
          Destroy(this);// destroys the script so you can't spam the door
         }
       }
    }
}

however the door only opens if the character is really really close to it, and also it only responds to touches on the touch pads in the bottom corners not when I just tap the door in the center of the screen. I'm guessing the character controller script is conflicting with my script. anyone any suggestions?

Dennis

p.s. I already posted this on the unity forum but people couldn't really help me there

more ▼

asked Dec 08 '11 at 10:44 AM

vengeance92 gravatar image

vengeance92
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest
var studioBlock : Transform; // blocker behind the door to stop you entering a room
private var doorObject : GameObject; // the door gameobject (has a collider)
var doorName = "door"; // name of the door gameobject
private var hit : RaycastHit;
private var ray : Ray;
private var touchpos : Touch;

function Start(){

doorObject = GameObject.Find(doorName);

}

function Update (){
       ray = Camera.main.ScreenPointToRay(Input.mousePosition); //I can't get touch.position to work
       //Debug.DrawLine(ray.origin,ray.direction * 1000);

       if(Input.GetMouseButtonUp(0))
       {
         if(Physics.Raycast(ray,hit,1000))
         {
          if(hit.collider.gameObject.name == "door")
          {
               Debug.Log("door");
               studioBlock.position.y = -100;
          }
         }
       }
}

here it is your answer. its working well... you're confusing much in your coding...you have to attach script into main camera...because check the "ray, you're making ray from main camera". no need of lot variable like string name and lot....

i tested this...and sure it'll work...

more ▼

answered Dec 08 '11 at 11:28 AM

sriram90 gravatar image

sriram90
460 33 37 47

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3748
x1968
x1535
x583
x39

asked: Dec 08 '11 at 10:44 AM

Seen: 2407 times

Last Updated: Dec 08 '11 at 11:28 AM