Hello Everyone
i Got a Huge Problem in Rotate a Texture
This Picture will explain what exactly i need
i have TheHitter And Arrow Texture
I Just Want The Arrow Spin[Rotate] To TheHitter Position, And Thanks
Hello Everyone
i Got a Huge Problem in Rotate a Texture
This Picture will explain what exactly i need
i have TheHitter And Arrow Texture
I Just Want The Arrow Spin[Rotate] To TheHitter Position, And Thanks
Attach this scrip to an empty game object. Drag and drop the game object you want it to follow onto goFollow. ‘tex’ is the texture you want to display. Note that the position in the transform should be set in screen coordinates, not world coordinates.
using UnityEngine;
using System.Collections;
public class RotateToPoint : MonoBehaviour {
public Texture2D tex; // Texture to be rotated
public GameObject goFollow; // GameObject to point to
private float angle = 0;
private Vector2 pos = new Vector2(0, 0);
private Rect rect;
private Vector2 pivot;
void Start() {
if (tex == null || goFollow == null) {
Debug.Log ("Texture or game object to follow is null");
return;
}
pos = new Vector2(transform.localPosition.x, transform.localPosition.y);
rect = new Rect(pos.x - tex.width * 0.5f, pos.y - tex.height * 0.5f, tex.width, tex.height);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
return;
}
void OnGUI() {
Vector2 guiPos = Camera.main.WorldToScreenPoint(goFollow.transform.position);
guiPos.y = Screen.height - guiPos.y;
Vector2 v2T = ((Vector2)guiPos - pivot);
angle = (Mathf.Atan2 (v2T.y, v2T.x)) * Mathf.Rad2Deg;
Matrix4x4 matrixBackup = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture(rect, tex);
GUI.matrix = matrixBackup;
}
}
This code is based off of code found in this post:
http://answers.unity3d.com/questions/11022/how-to-rotate-gui-textures.html