- Home /
Rotating a GUI Texture depending on a Collision
Hello everyone,
I have made a GUI texture for a semi circle with an arrow at the top of the semi circle.
I have made it able to be rotated arund a point using this code:
var Targeter : Texture2D; var Gdepth : int = 0; var rotAngle : float = 0; private var pivotPoint : Vector2; var Scaler = 0.00; var alpha = 1.00;
function OnGUI() { GUI.color = new Color(1,1,1, alpha); GUI.depth = Gdepth; pivotPoint = Vector2(Screen.width/2,Screen.height/2); GUIUtility.RotateAroundPivot (rotAngle, pivotPoint); GUI.DrawTexture(new Rect ((Screen.width/2)-(50*Scaler), (Screen.height/2)-(62*Scaler), (50*Scaler)*2, (50*Scaler)), Targeter, ScaleMode.ScaleToFit);
rotAngle ++; }
however instead of just rotating I want to be able to make the rotation of it equal to a value that is dependent on the point of collision of a bullet with my character collider
For example if the bullet collider that hit me hit my collider at the front the GUI would have a rotation of 0
if I was hit from the left, rotAngle = 270
if I was hit from the right, rotAngle = 90
if I was hit from behind, rotAngle = 180
I am guessing I would use something like ContactPoint, Collision.contacts or ControllerColliderHit.point
but I'm not sure on the best way forward
any help would be much appreciated
Thanks Scribe
Answer by Scribe · May 17, 2011 at 12:27 PM
the whole script I ended up with:
var Targeter : Texture2D; var TargeterCube : Transform; var Gdepth : int = 0; private var pivotPoint : Vector2; var Scaler = 1.00; var alpha = 0.00; var rotAngle : float; var smooth = 2.0; var delayT : int;
function Update () { rotAngle = TargeterCube.eulerAngles.y;
if(delayT == 0){
CancelInvoke();
alpha = 0.0;
}
}
function OnCollisionEnter () { alpha = 1.0; delayT = 2; InvokeRepeating("Minus1", 1, 1); }
function OnGUI() {
GUI.color = new Color(1,1,1, alpha);
GUI.depth = Gdepth;
pivotPoint = Vector2(Screen.width/2,Screen.height/2);
GUIUtility.RotateAroundPivot (rotAngle, pivotPoint);
GUI.DrawTexture(new Rect ((Screen.width/2)-(50*Scaler), (Screen.height/2)-(62*Scaler), (50*Scaler)*2, (50*Scaler)), Targeter, ScaleMode.ScaleToFit); }
function Minus1 () { delayT -= 1; }
and the part that actually does the rotating:
GUIUtility.RotateAroundPivot (rotAngle, pivotPoint);
hope that might help someone looking for the same answer as I was
Scribe
Your answer
