Hey,
Ive made some previous topics on similar subjects for the past few days, and I have gotten a little further in my quest for arrow gui pointing to at touch position like a clock. The problem I have now is that the arrow is not rotating on its pivot correctly (the way I want it; like a clock, is the pivot in the middle by default or not?) check the vid out:
http://www.screencast.com/t/ZGE0MTkyY
Here is the code if you can help:
@script RequireComponent (Texture2D)
var arrow: Texture2D;
function OnGUI(){
//check for touch
var touch : iPhoneTouch = iPhoneInput.GetTouch(0);
//denotes the position of the Texture
var arrowX : float = 60;
var arrowY : float = 240;
//places both gui positions in a controllable vector variable
var thePos = Vector2 (arrowX, arrowY);
//calculates the radian of the positions
var theRad = Mathf.Atan2(touch.position.x -arrowX, touch.position.y - arrowY);
//calculates angle
var theAng : int = theRad * Mathf.Rad2Deg;
//pos of texture plus height and width
var theRect : Rect = Rect (arrowX,arrowY,70,70);
//Rotate around the pivot postion
GUIUtility.RotateAroundPivot (theAng, thePos);
//shows the arrow texture gui
GUI.DrawTexture(theRect, arrow);
}
The other thing I want to do is put a restriction on the player by not letting them able to touch anywhere on the screen to rotate the arrow but touching inside the skull boundary itself to move the arrow.
Thanks
SOLVED:
I used a plane with a texture on it instead of Texture2d. Here is a vid:
http://www.screencast.com/t/YmE3MzQzMTgt
Here is the code if anyone wants to use it:
function Update(){
//check for touch
var touch : iPhoneTouch = iPhoneInput.GetTouch(0);
//gets values for the default resolution and modify screen width/height to flip in direction
var screenX = Screen.width * -1;
var screenY = Screen.height * -1;
//denotes the position of the of the gameo
var arrowX : float = -180;
var arrowY : float = -100;
var theP = Vector2 (arrowX, arrowY);
//gives the coordinates of player touch with middle of screen being (0, 0)
var touchP = Vector2 (screenX + touch.position.x + 240, screenY + touch.position.y + 160 );
//position relative to objects coordinates
var relative = Vector3 (touchP.x - arrowX, touchP.y - arrowY);
//calculates the radian of the positions
var theRad = Mathf.Atan2(relative.x,relative.y);
//calculates degrees of the radian
var theDeg : int = theRad * Mathf.Rad2Deg;
//reverse the angle
var theAng = theDeg * -1;
//rotate the object by angle on z axis
transform.rotation = Quaternion.AngleAxis (theAng, relative.forward);
Debug.Log (relative);
}
At the end of the vid, you can notice the Unity message “UnityException: Index out of bounds”. What does it mean?
The index out of bounds is most likely caused by the GetTouch(0) at the top of the Update function. If there is no finger on the screen, then there isn’t a touch number zero. You should use iPhoneInput.touchCount to check there is at least one touch before using GetTouch.
There is solution for RotateAroundPivot and it handy to work with Texture2D needles, and the sample code is below…
function OnGUI ()
{
GUI.skin = guiSkin;
var needleX : float = needleOffset.x;
var needleY : float = needleOffset.y;
var needlePivotPoint = Vector2 (needleX + needlePivotPointXOffset, needleY + needlePivotPointYOffset);
var needleRect : Rect = Rect (needleX, needleY, needleTexture.width, needleTexture.height);
GUIUtility.RotateAroundPivot (needleAngle, needlePivotPoint);
GUI.DrawTexture(needleRect, needleTexture);
}
Thanks. I’ll think about using that too. I’ve also updated my original texture rotation script…I’ll post later.