Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Daniel 1 · Jan 17, 2010 at 04:48 PM · raycastinputiphone

Simple (?) touch-command on iPhone

Ive done the fps-tutorial a while back so I have some idea of how scripts work, but when it comes to the iphone-specific touchcommands Im a bit stumped.

Im simply trying to find a way to interact with the environment by clicking on various objects in the scene, and the first step in that process has been to try and figure out how this code-snippet retrieved from the docs works and how to implement it. Its supposed to just trigger a particle once the screen is touched, but I need it to do so only when the target object (lets say a cube) is touched.


var particle : GameObject; 
function Update () {
    for (var touch : iPhoneTouch in iPhoneInput.touches) {
        if (touch.phase == iPhoneTouchPhase.Began) {
            // Construct a ray from the current touch coordinates
            var ray = Camera.main.ScreenPointToRay (touch.position);
            if (Physics.Raycast (ray)) {
                // Create a particle if hit
                Instantiate (particle, transform.position, transform.rotation);
            }
        }
    }

}

This is how I think the script works: In every frame a iPhoneTouch-object (touch) is created, which checks if the touch is a beginning touch. If so a ray is created from the camera to the touched position, and finally the particle-effect is triggered.

Is this correct, or have I misunderstood the function?

And if it is correct, how do I make it check if the ray has hit an object with a response?

And finally, where do I put this script? Should it be attached to each object that should respond to it (which would mean a lot of scripts running), or should it be attached to the player and have it check for each possible object the ray could hit with an if-else-structure?

Also, if you have a better codesnippet that incorporates not just one aspect that would be most welcome since codesnippets tend to be very illustrative and helpful in understanding concepts.

Thanks, Daniel

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
1
Best Answer

Answer by Bampf · Jan 17, 2010 at 08:11 PM

Physics.Raycast returns True if the ray hits any collider in the scene. If the object you are testing has a collider on it, and there are no others in the scene, then you should be good to go.

Otherwise, you can use layers to group colliders into those you are testing and those you want to skip. (Layer mask is an optional parameter to Physics.Raycast.) Or you can simply check the hitInfo structure that Raycast can give you to see what was hit (see docs, it depends which version of Raycast you call.)

The array of touches is good particularly if you want to respond to multi-touch, but if you are only interested in single touches you can simply use Input.GetButtonDown("Select"), which detects mouse clicks in Unity, and touches in Unity iPhone. (The argument to GetButtonDown is the name of a virtual button. You can define your own but "Select" is one that exists by default, and it maps to Mouse Button 1 in Unity, and a single touch in Unity iPhone. Here is a link to an overview of the Input manager.)

Comment
Add comment · Show 3 · 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 Daniel 1 · Jan 17, 2010 at 09:03 PM 0
Share

Is this the correct usage?

1) I create an object and name it "Door1".

2) I attach a script to the player's update-function as follows:

function Update () { if (Input.GetButtonDown ("Door1")) { // code for switching scene to "Scene_1", for instance } }

So every time I make a touch in the game it will check wether the "Door1"-object has been pressed and execute the script.

avatar image Bampf · Jan 18, 2010 at 11:56 AM 0
Share

Putting it in the Update function is correct, but Input.GetButton("Select") is just detecting that a mouse click or touch has occurred. (Note: "Select" is the name of a virtual button; you can define new ones but that's a different topic. I've added documentation links about that to my answer above.) After teh click or touch has happened you still need the raycast to deter$$anonymous$$e if it hit anything.

avatar image burnumd · Oct 26, 2010 at 06:58 PM 1
Share

Also, rather than handling the logic for doing stuff when something is hit by the player within the player class, have a look at Send$$anonymous$$essage http://unity3d.com/support/documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html and make the thing that was hit responsible for deter$$anonymous$$ing what happens.

avatar image
0

Answer by cregox · Jan 28, 2011 at 06:01 PM

this:

// OnTouchDown.cs // Allows "OnMouseDown()" events to work on the iPhone. // Attach to the main camera.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class OnTouchDown : MonoBehaviour { void Update () { // Code for OnMouseDown in the iPhone. Unquote to test. RaycastHit hit = new RaycastHit(); for (int i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) { // Construct a ray from the current touch coordinates Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if (Physics.Raycast(ray, out hit)) { hit.transform.gameObject.SendMessage("OnMouseDown"); } } } } }

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 lion-gv · Feb 21, 2011 at 06:07 PM 0
Share

I find the OnTouchDown.cs camera script adds a 1 second input delay on an iOS device, any idea why this might be happening?

avatar image cregox · Feb 21, 2011 at 07:34 PM 0
Share

@lion-gv sorry, I have no idea. but I get no delay on my end, so I'd suggest you trying an empty project, or even an empty scene without any other scene being loaded just to be sure, and try to add things little by little so you can diagnose where the issue might be. this code is not processor intensive so it shouldn't be bringing any issues on its own.

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

No one has followed this question yet.

Related Questions

iPhone touch events slowdown fps?!? 2 Answers

OnCollisionEnter-issue 1 Answer

Working with Touch screens... 1 Answer

Ray error with touches.position 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 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