Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by D3m0nE · Feb 26, 2013 at 10:09 PM · guitexturerotatespin

Rotate Texture

Hello Everyone i Got a Huge Problem in Rotate a Texture

This Picture will explain what exactly i need

alt text

i have TheHitter And Arrow Texture

I Just Want The Arrow Spin[Rotate] To TheHitter Position, And Thanks

gui_arrow.jpg (21.4 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 26, 2013 at 10:27 PM 0
Share

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?

avatar image D3m0nE · Feb 26, 2013 at 11:34 PM 0
Share

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

avatar image Lockstep · Feb 26, 2013 at 11:51 PM 0
Share

Use GUIUtility.RotateAroundPivot to rotate the GUITexture. The rest is maths.

avatar image D3m0nE · Feb 27, 2013 at 01:02 AM 0
Share

@Lockstep Thanks But In that GUIUtility Where i should put TheHitter Position or rotation. Sorry But i rly Cant Get it

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image D3m0nE · Feb 27, 2013 at 02:25 AM 0
Share

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

avatar image robertbu · Feb 27, 2013 at 03:03 AM 0
Share

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.

avatar image D3m0nE · Feb 27, 2013 at 12:18 PM 0
Share

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 :)

avatar image robertbu · Feb 27, 2013 at 03:31 PM 0
Share

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.

avatar image D3m0nE · Feb 28, 2013 at 08:23 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Make more buttons appear, on button click. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges