Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vengeance92 · Dec 08, 2011 at 10:44 AM · iosraycasttouchtap

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CoolWong · Jun 29, 2014 at 10:34 PM 0
Share

Thanks for this help. Does it work in 2D game and Input.touchPosition? I've try but the raycast never hit the 2D object :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sriram90 · Dec 08, 2011 at 11:28 AM

 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...

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gulfam131 · Nov 16, 2013 at 06:09 PM 0
Share

its awesome.... working perfect.

avatar image gulfam131 · Nov 16, 2013 at 06:10 PM 0
Share

very helpfull, you have saved alot of my time. Thank you so much. :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect a tap versus standing touch on iPhone? 3 Answers

How to push object along Z axis with Ray on touch? 1 Answer

Make an object move in the direction of touch 0 Answers

How to select an object with TOUCH and change its animation 2D 1 Answer

A node in a childnode? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges