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 Der_Kevin · Nov 02, 2013 at 02:28 PM · meshinputkeyboard

Trigger Event with Pressing on a Mesh

Hey guys!

I just have a quick question. I have a small script that allows the player to enter a car. nothing special. it uses an empty game object with a Cube (Mesh Filter) and if the player is running around in this Mesh and presses "F" he will enter the car. Buuuut, I want to use this script for mobile and as you all know that there is no "F" button on the iphone.

So, my idea is, that you have to tap on the Cube to enter the car instead of pressing "F" currently the script is working like that (javascript):

 function Update()
 {
 if (Input.GetKeyUp("f")&& isPlayerVisible) //Asign any key you want to enter/operate vehicle.
 {
 Debug.Log("Driving");

so, how can i replace the Input.GetKeyUp("f") with something like Press.GameObject("EnterCar") or how ever this will work?

Thanks in advance! Kev!

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

2 Replies

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

Answer by Zaeran · Nov 02, 2013 at 02:55 PM

Camera.main.ScreenPointToRay(ScreenTouchPosition) will give you a ray pointing through the camera to the position touched on the screen.

Pass the ray into a raycast, then analyse what gets hit. If it's the car, then get in.

Relevant code:

 Ray screenRay = Camera.main.ScreenPointToRay(ScreenTouchPosition);
 RaycastHit rayhit;
 if(Physics.Raycast(screenRay, out rayhit)){
     if(rayhit.collider.tag == "CarCube"){ //whatever you want to call it
         Debug.Log("Driving");
     }
 }
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 Der_Kevin · Nov 03, 2013 at 03:47 PM 0
Share

Hey, thanks a lot :) i just have some questions, just to understand the code and learn from that :)

  1. so, Camera.main is always the Camera? even if my Camera has the Name "tk2dCamera_Player" but its still Tagged as main camera?

  2. and, (ScreenTouchPosition): am I still able to test the scene within the unity player even if the game is not already on my phone or should i change it to something like (Screen$$anonymous$$ousePosition) when i want to test it with my mouse cursor?

  3. and, "CarCube" is the name of the GameObject right? so in my case it would be "doorTriggerLeft" but i just have to replace it ere right?

Thanks a lot!

avatar image Zaeran · Nov 03, 2013 at 04:09 PM 0
Share
  1. Camera.main is whichever camera is tagged as $$anonymous$$ainCamera, regardless of the name.

  2. As far as I know, Unity doesn't have any built in method to test touches, so you'll probably have to use Screen$$anonymous$$ousePosition.

  3. You're correct. CarCube is the name of the object, and you replace it with the name of the object you want to click on

avatar image
0

Answer by Der_Kevin · Nov 03, 2013 at 07:03 PM

ok, thanks, one more ;) now the code looks like this:

     function Update()
     {
     Ray screenRay = Camera.main.ScreenPointToRay(ScreenTouchPosition);
     RaycastHit rayhit;
     if(Physics.Raycast(screenRay, out rayhit)){
         if(rayhit.collider.tag == "doorTriggerLeft")
     {
     Debug.Log("Driving");

but unity says "Assets/Standard Assets/Scripts/Car entering & exiting.js(16,4): UCE0001: ';' expected. Insert a semicolon at the end."

16,4 is between Ray and screenRay could you help me with that? :)

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

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

16 People are following this question.

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

Related Questions

Checking whether string is a valid Input.Key 1 Answer

setting iPhoneKeyboard.text (or any workaround about resetting user input) in Android. 0 Answers

Android Keyboard .text String returning empty? 1 Answer

How to prevent input lag/delay? 1 Answer

Keyboard entry for puzzle game battles 0 Answers


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