- Home /
Rotate Texture
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
Is this 2D? Are you looking along the Z axis or down the Y axis? Are you trying to use GUI, or are you using a scene object for the pointer?
TheHitter Is 3D [Game Object (Player)]
The Arrow is 2D [GUI Texture]
i just need the arrow Point[rotate - Spin] to the Target
i want to use that on FPS Game
When Enemy Hit $$anonymous$$e. The Arrow Will Show and tell me where did i get hit from
Use GUIUtility.RotateAroundPivot to rotate the GUITexture. The rest is maths.
@Lockstep Thanks But In that GUIUtility Where i should put TheHitter Position or rotation. Sorry But i rly Cant Get it
Answer by robertbu · Feb 27, 2013 at 01:17 AM
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
at first i would like to thank you
but the code didnt work for 2 reason
1- i cant see the texture. 2- the game object, when i start the game it gave me error its says no game object assigned
I Called Your Script "WhereIGotHitted"
and from the Damage Script i use
WhereIGotHitted hittedscrypt = GetComponent<WhereIGotHitted>();
hittedscrypt.goFollow = attacker;
attack = the player who hit me
First, you likely cannot see the texture because you have to set the position of the empty game object to screen coordinates. So in the inspector in the Transform section, set the position to something like (500,500,0) to start.
For the second issue, just delete the "|| goFollow == null" in the if statement on line 14.
set position for arrow? -.-
Its Should Be automaticly sit at the position of TheHitter...
and for second issue When TheHitter Hit $$anonymous$$e
i Get This Error of setting the GameObject "goFollow"
NullReferenceException: Object reference not set to an instance of an object
...
i tried to set "goFollow" Object $$anonymous$$anually inside the game
No Error shows and No Texture Shows :)
I tested this code before I posted it, so I'm not sure what is going on with your texture does not showing. Try the code exactly as posted in a new scene with just a block for the player and see if you cannot get your texture to work there first. You can move the block while the game is running by editing the transform position values.
I Tested It On New Empty Project And Its works like charm ;))
okey i will work on it in my project :)
thanks.. but there i little thing i wish u can do it for me
that the arrow just rotate . not move
i mean i need if i got hitted from right UP position , then the arrow will show on the right Up
And if he hit me from Back , then the arrow will be Down middle the screen
and Thanks
Your answer
Follow this Question
Related Questions
GUIUtiliy.RotateAroundPivot() doesn't work 0 Answers
Limit GUI Rotation? 2 Answers
Spin a texture in just any material -is it possible? 1 Answer
Help with destroying guiRect? 0 Answers