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
1
Question by LordZephyr · Dec 27, 2011 at 12:58 AM · iosscenetouchchange

Scripting a "touch" on an object in iOS.

I'm sure this is a simple fix to most of you but I have been racking my brains out trying to figure it out.

I am working on a first person game and I want to be able to change scenes when the player comes to a door (cube) and touches it with his finger. I have been working on this script:

 function Update (){
 
    if(Input.GetMouseButtonUp(0))
    {
            Debug.Log("I touched the wall!"); //I will be changing the Debug.Log to a
                                          // scene change when it is working properly.
    }

}

but all I seem to get is a printout in the console every time the player's finger is taken off the screen... no matter what he touches. I would like it to happen only when the player touches a particular cube that looks like a door.

I would really appreciate any help that anyone may be able to supply. Thanks so much.

Tom

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hendrys · Dec 30, 2011 at 12:15 AM

Don't omit the RayCast part, it's important. A ray is a mathematical (invisible) line. In this case this ray is calculated based on the touch's position and the camera's projection using the function ScreenPointToRay. This function "projects" the point (in the device's 2D screen) to a Line in the world, starting from the Camera's position and until it reaches 100 units "into" the device.

function Update () { for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { //Here you have the touch's position so, This is where you have to create a ray using ScreePointToRay var userRay = Camera.main.ScreenPointToRay(touch.position) ; //This is to store the RayCasting results var info : RaycastHit ; //[Physics.RayCast][2] actually casts the ray and if it returns true it "Hit" something if(Physics.Raycast(userRay, info, 100.0f)) { //This tells us if the thing it hit was a "Door". if(info.transform.CompareTag("Door")) { // This is where you have to put the change level info Debug.Log ("I touched the door!"); Application.LoadLevel ("Level2");

   }

  }

} }

}

It looks awful and tedious but that's what you have to do to hit something. Also, this looks terrible becasue the comments, but you can do it cleaner if you prefer. But please do not take away the Raycast part.

Comment
Add comment · Show 5 · 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 LordZephyr · Dec 30, 2011 at 12:59 AM 0
Share

@Lo0Nuhti$$anonymous$$, I believe @Hendrys was talking about me. I left out the Ray section because I wanted to try to see if I could get a simple touch to work if I put the script on the cube itself. Well, it didn't work, of course. I also wasn't sure hot to incorporate both the touch and the ray in the script.

@Hendrys, I coded exactly as you posted and I received a "touchRange" error in the script for the line if(Physics,Raycast(userRay, info, touchRange)). Am I missing a variable? I don't know why I am getting this error, now.

I am so sorry for being such a pest but I am so close to finishing this game. When I'm done, I promise to learn all about rays... and as much about scripting as I can. LOL.

Thanks so much for your help! $$anonymous$$

avatar image Hendrys · Dec 30, 2011 at 01:06 AM 1
Share

Yeas @Lo0Nuhti$$anonymous$$ i was talking to @Zephyr, and that error you get is because you have to declare the touchRange variable. var touchRange: float = 100.0f

avatar image LordZephyr · Dec 30, 2011 at 02:55 AM 1
Share

YOU GUYS ARE AWESO$$anonymous$$E! It is working and I can't thank you all enough!!! @Hendrys, @Lo0Nuhti$$anonymous$$, and @crazy$$anonymous$$night! Thank you so much. I can't tell you how much this means to me. I'm off to learning so much more about Unity and scripting. I hope I can help you guys some time in the future. And thanks for being so patient with me.

$$anonymous$$

avatar image LordZephyr · Dec 30, 2011 at 03:31 AM 1
Share

Also, thank you for guiding me through the proper etiquette of the Unity Answers and all its tricks. I truly appreciate it.

avatar image RDC · May 15, 2014 at 07:04 AM 0
Share

@Hendrys.. I really appreciate you answer but I wish i could get the same in C# script way.. anyway Thanks.

avatar image
2

Answer by Hendrys · Dec 27, 2011 at 11:58 AM

The problem is in IOS you don't have a Mouse, you have to use input.GetTouch(). Then you have to get the position of that touch with input.GetTouch().position. Then you have to Cast a Ray from the User's perspective (the camera) to that position in the World (using Camera.SreenPointToRay() )

And then test if the Ray hits the Cube (door).

If that happens then you change the scene

Try this:

 //A Vector to store the touch position
 Vector2 userClic;
 
 //If the user has touched anywhere...
 if(Input.touchCount > 0)  
     //Then, store the touch position
     userClic = Input.GetTouch(0).position;
 
 //Create a ray base on the camera's position and the userClic
 Ray userRay =  Camera.mainCamera.ScreenPointToRay(userClic);
 
 //an object to store the ray cast info
 RaycastHit info;
 
 //If the ray hits any object...
 if( Physics.Raycast(userRay,out info, 100))
 {
     //If that object is a Door (You have to label the object "Door")
     if(info.collider.tag == "Door")
     {
      //Here you open your door i.e. change the scene...
     }
 }



Hope it helps...

Comment
Add comment · Show 1 · 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 LordZephyr · Dec 28, 2011 at 08:49 PM 0
Share

@Lo0Nuhtik, Thank you for the translation. It seemed to look right as far as formatting. I put it in and received an "ArgumentException: You are not allowed to call INTERNAL_CALL_ScreenPointToRay when declaring a variable." I'm sorry but I am not familiar with "Rays" yet to fix this myself. Any help would be appreciated. Thanks so much. $$anonymous$$

avatar image
0

Answer by crazyKnight · Dec 27, 2011 at 08:55 AM

 var hit: RaycastHit;
 var ray;
 
 function Update () 
 {
     if(Input.GetMouseButtonDown(0)) 
        {
          if (Physics.Raycast (ray, hit, 100)) 
            {
               if(hit.collider.gameObject.tag == "Door")  // tag your cube with the name Door
                 {
                  // write in your code here 
               }
        }
 }
Comment
Add comment · Show 8 · 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 Hendrys · Dec 27, 2011 at 02:07 PM 1
Share

excuse me, why my answers aren't appearing at all? Could you moderate them? do you have enough karma?, thanks a lot.

avatar image LordZephyr · Dec 27, 2011 at 02:42 PM 0
Share

I'm terribly sorry but I don't understand why your answers aren't showing up. I don't believe I have any control over that. I'm relatively new on Unity Answers so I don't have much karma. I don't know if it is about my karma or yours. I'm sorry I can't help in this but thanks for trying to help me. I appreciate it.

avatar image Hendrys · Dec 27, 2011 at 02:49 PM 1
Share

no, that's ok @Zephyr i was talking to @crazy$$anonymous$$night but FYI you need 15 karma in order for your question not to need moderation when posted. I do have 32 karma now, so everything is ok. bye :)

avatar image LordZephyr · Dec 27, 2011 at 03:21 PM 0
Share

Dear crazy$$anonymous$$night, I put in the name of the door in the tag of the cube under Element 0 and dragged the script onto the cube that is the door. When I remotely played the game to see if the script worked, I received an error in the console stating "NullReferenceException: Object reference not set to an instance of a TouchTest.Update() (at Assets/TouchTest.js:9). Did I do something wrong?

avatar image LordZephyr · Dec 27, 2011 at 03:29 PM 0
Share

Dear @Hendrys, I typed in your code suggestion (I think I needed to add the { }s after the "if(Input.touchCount > 0) because they weren't there.) but I couldn't try it because I am getting an error stating that a ';' is expected at line 'Vector2 userClic;' but I already have one there. I'm so sorry to bother you with these little problems but I am at a total loss with this one. Thanks so much for your help. Both you and @crazy$$anonymous$$night.

Show more comments

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

8 People are following this question.

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

Related Questions

Laggy gameplay and occasional crashes 1 Answer

Is there an OnSceneChange event? Or some kind of event that is triggered when a new scene is loaded? 2 Answers

Player object carried from scene to scene 1 Answer

Move camera upon button press 0 Answers

Having 10 Levels in 1 Scene? 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