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
2
Question by Marashu · Jul 13, 2010 at 02:19 PM · movementiphoneraycasthit

iPhone Touch RayCastHit problem

Howdy, all.

I'm making a 3d hockey game for the iPhone, and I'm having a problem with my movement. I want it so that the active player moves to the place last touched. However, after a little while of playing, the z co-ordinate of the hit will wind up somewhere below the screen. I've made it output the screen positions of the touch, and that's not the problem. Any suggestions on how I should approach this problem? gameInfo.touchPoint is just a Vector3 I made for the GUI so that I can see the hit position in the GUI until this is solved. What's going on is that I'll touch a point that has a z of 5, and suddenly it'll think I'm touching at -13, then eventually bring itself back to 5. After this code is where I do my vector subtraction to get the character moving, but it seems like the problem is with the hit itself.

EDIT I added some script to trace out what I'm touching (gameInfo.touchObject = hit.transform.name; and then stuff to draw it through the GUI), and I'm still touching the ice even as this is happening. In another test, I rotated the camera so it was directly overhead, and this continued to happen.

Here's the code in question:

var count : int = iPhoneInput.touchCount; if( count == 1 && (state == ControlState.MovingCharacter) ) { var touch : iPhoneTouch = iPhoneInput.GetTouch(0);

 if (  touch.phase != iPhoneTouchPhase.Began )
 {
     if(touch.deltaPosition.magnitude > 70)
     {
         moving = false; 
         targetLocation = thisTransform.position;
     }
     else
     {
     var ray = cam.ScreenPointToRay( Vector3( touch.position.x, touch.position.y ) );
     gameInfo.touchPoint = touch.position;
     //Raycast to move the player
     var hit : RaycastHit;
     if( Physics.Raycast(ray, hit) )
     {
         //Check if the user has selected the Ice or another player, then allow movement to that location
         if((hit.transform.gameObject == iceGameObject) || (hit.transform.gameObject == puckGameObject)){
             var touchDist : float = (transform.position - hit.point).magnitude;
             if( touchDist > minimumDistanceToMove )
             {
                 targetLocation = hit.point;
                 gameInfo.hitPoint = targetLocation;
                                         gameInfo.touchObject = hit.transform.name;
                 moving = true;
             }

         }

         else{
             Debug.Log("hit: "+hit.transform.parent.name);
             moving = false;
         }
     }
     }

 }
 else
 {
     moving = false;
     //targetLocation = thisTransform.position;
 }

}

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 spinaljack · Jul 13, 2010 at 11:01 PM 0
Share

you can get it to print the name of the object you're hitting so you can see what's causing it.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Thom Denick · Jul 14, 2010 at 04:34 PM

As spinaljack suggests, the best way to find out why a Ray isn't triggering is to return name of the object your Ray is hitting. This can help your debug any confusing collision problems or problems with your code.

Here's a simple way to return what the raycast hits:

//Change to iPhone Touch obviously. if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;

     if (Physics.Raycast(ray, out hit)) {
         Debug.Log("Hit: " + hit.transform.parent.name);
     }
 }   

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
avatar image
0

Answer by jtbentley · Aug 29, 2010 at 03:05 AM

I would also ask about the type of colliders you have. Is the collider for your ground a plane or a box? Plane's are expensive for collision ;)

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

No one has followed this question yet.

Related Questions

Is it possible to have my character latch to an object? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to Modify Multiplayer Script for iPhone 0 Answers

Camera/mouse movement issues "almost but no dice" 1 Answer

unity iphone raycast problem 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