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 Bentoon · Nov 27, 2014 at 08:54 PM · c#rotationmath360quads

Quad rotation 360º switching textures

Hello all I have an upright plane (actually it’s a Quad) that turns around it’s Y Axis to face the camera (all good so far) As you move around it, it spins 360, and the textures change every few degrees to give you the evict of a 360 product shot ( good so far) But with my Code (below) I am only going half way around, before it resets So I am only getting half the images With the debug.Log I am seeing that the index # only goes up to 12 (sometimes 13) though I have 27 images

index 0 more or less equals -1 to -21 index 1 more or less equals -23 to -48 index 2 more or less equals -50 to -75 index 3 more or less equals -80 to -100 index 4 more or less equals -100 to -130 index 5 more or less equals -133 to -156 index 6 more or less equals -160 to - -180

But @ index 6 it goes from -180 to positive 180

index 7 more or less equals 184 to 203 index 8 more or less equals 208 to 237 index 9 more or less equals 240 to 260 index 10 more or less equals 260 to 300 index 11 more or less equals 303 to 330 index 12 more or less equals 330 to 356 @ index 12 = its 359 and @ index 13, very briefly, it’s switching back to negative numbers and starting @ -1

Here's the code, commented: using UnityEngine; using System.Collections;

 public class LookAt3 : MonoBehaviour {
     
         
     public Transform player;
 
     public Texture[] textures;
 
     public int numbersDevide360; 
 
         
         void Start() 
         {
         player = GameObject.FindGameObjectWithTag("Player").transform;
         float angle = 0.0f;
         int index = 0;
 
         }
         
 
 
         void Update() 
         {
     // Plane faces the Player
             Vector3 v3 = player.position - transform.position;
             v3.y = 0.0f;
             transform.rotation = Quaternion.LookRotation(-v3);
 
     //  Sending the transform.rotation to the console in hopes of finding something I could use...
     //    Debug.Log (transform.rotation);
 
         ChangeTexture();
         }
 
 
 
     void ChangeTexture()
     {
                 Vector3 dir = transform.position - player.position;
                 //float angle = Mathf.Atan2 (dir.x, dir.z) * Mathf.Rad2Deg;
         float angle = Mathf.Atan2 (dir.x, dir.z) * Mathf.Rad2Deg;
         if (angle > 0.0f) {
             angle = 360.0f - angle;
         }
                 Debug.Log ("Angle = " + angle);
 
                 //int index = (int)angle / numbersDevide360;
                 int index = (int)angle /textures.Length;
 //        if (index > textures.Length) {
 //            index = 0;
 //        }
 
 
 
         //    if less than 0  then absolute it and muultiple it by 2
 
                 
                  index = Mathf.Abs (index);
                 Debug.Log ("index = " + index);
             if (index <= textures.Length) {
                 GetComponent<Renderer> ().material.mainTexture = textures [index];
                 }
         }
 
 }


So,

What I want is the 360 rotation to go all the way through the # of texture

& allow for the # of textures I have

(which may change but most of the time under 30) way more than 12 I seem limited to

Thank You in Advance

& Happy L-Tryptophan!

~be

Comment
Add comment · Show 5
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 FairGamesProductions · Nov 28, 2014 at 01:09 AM 0
Share

So, right off the bat, I can see one problem with the use of textures.Length

textures.Length WILL give you the length of the array, BUT, for example, if you have 10 textures, the length WILL be 10, BUT they are numbered from 0 to 9. So you should use:

 if (index > (textures.Length - 1))

And:

 if (index < textures.Length) //Notice there is no =
avatar image Bentoon · Nov 28, 2014 at 05:23 AM 0
Share

Thanks FairGames,

But nothing changes The first line you site is commented out and doesn't have any bearing, The second doesn't do anything either.

I'm only getting 12 or 13 texture changes no matter what I do

... Funny

~be

avatar image robertbu · Nov 28, 2014 at 06:01 AM 0
Share

Line 41 should be:

  if (angle < 0.0f) {
avatar image Bentoon · Nov 28, 2014 at 07:53 AM 0
Share

Thank you RobertBu It's interesting but actually jumps more.

index 1 through 6 work well moving from 0º to 180º

but then it jumps to index 19 @ 540º to index 13 @ 360º

Then repeats back to index 1 @ 0º etc

So the textures change smoothly 6 times each way and then jump as opposed to 12 sequential and then jumping just with the >< change

But it is still only reading 12 steps as opposed to 27

because 27 images in textures.Length divided into 360 is around 13

Thanks!

~be

avatar image Bentoon · Nov 28, 2014 at 08:15 AM 0
Share

Great! Thank you RobertBu (once again : )

I haven't tried this yet but I got it working with this below:

 using UnityEngine;
 using System.Collections;
 
 public class LookAt4 : $$anonymous$$onoBehaviour {
     
         
     public Transform player;
 
     public Texture[] textures;
 
     // mySteps is the # I get when I divide 360 by the nymber of Textures in my array 
     public int mySteps; 
 
         
         void Start() 
         {
         player = GameObject.FindGameObjectWithTag("Player").transform;
         float angle = 0.0f;
         int index = 0;
 
         }
         
 
     void Update() 
     {
         // Plane faces the Player
         Vector3 v3 = player.position - transform.position;
         v3.y = 0.0f;
         transform.rotation = Quaternion.LookRotation(-v3);
         
         ChangeTexture();
     }
 
 
     
 
     void ChangeTexture()
     {
                 Vector3 dir = transform.position - player.position;
                 //float angle = $$anonymous$$athf.Atan2 (dir.x, dir.z) * $$anonymous$$athf.Rad2Deg;
         float angle = $$anonymous$$athf.Atan2 (dir.x, dir.z) * $$anonymous$$athf.Rad2Deg;
         if (angle > 0.0f) {
             angle = 360.0f - angle;
         }
                 Debug.Log ("Angle = " + angle);
                 
         //int index = (int)angle /textures.Length;
         int index = (int)angle /mySteps;
 
                 
                  index = $$anonymous$$athf.Abs (index);
                 Debug.Log ("index = " + index);
             if (index < textures.Length) {
                 GetComponent<Renderer> ().material.mainTexture = textures [index];
                 }
         }
 
     } 
 

1 Reply

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

Answer by robertbu · Nov 28, 2014 at 08:07 AM

You should never see a value of 540 degrees. Mathf.Atan2() returns values in the range of -180 to 180, so this comparison and subtraction from 360, should normalize the values between 0.0 and 360.0.

And line 47 is wrong. I should be:

  int index = (int)(angle /(360.0f / textures.Length));

You can calculate 360/textures.Length in Start().

And if you get the code right, these two lines will not be necessary:

    index = Mathf.Abs (index);


    if (index <= textures.Length) {
Comment
Add comment · Show 1 · 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 Bentoon · Nov 28, 2014 at 08:19 AM 0
Share

ins$$anonymous$$d of dividing 360 by the the Number of Textures [27] which was only giving me 12

I needed to divide 360 by the 12 to give me 12

even though it's not great code, I used another variable mySteps = 12 to get 27 and Now it works

YES ! RobertBu - That's IT! you did the same thing but with much better more flexible code

Thanks for All your help RB !

If you officially "Answer" I can accept it

~be

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotation math problem 1 Answer

How to determine if rotation is clockwise or anticlockwise 2 Answers

Trouble with Camera Rotation Math 2 Answers

Rotation using exponential functions 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