- Home /
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;
}
}
you can get it to print the name of the object you're hitting so you can see what's causing it.
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);
}
}
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 ;)