- Home /
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
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 =
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
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
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];
}
}
}
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) {
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
Follow this Question
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