- Home /
360 degree plane rotation & texture change
Hello all,
These forums have been really helpful, especially Robertbu,with a question I had about switching textures on an upright plane
The plane is rotating 360 degrees around the Y Axis and
The plane switches its texture depending in the angle
but the plane is following a character and only reading 0 to 180 degrees and then back down (even though it is rotating 360 as the player moves around ) ,
so the texture are only going halfway (180deg) and then back down.
here's the code:
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;
}
void Update()
{
// Plane rotates to face 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 = Mathf.Atan2 (dir.x, dir.z) * Mathf.Rad2Deg;
//int index = (int)angle / numbersDevide360;
int index = (int)angle /textures.Length;
GetComponent<Renderer> ().material.mainTexture = textures [index];
}
}
Any ideas? Thanks in advance
Answer by robertbu · Nov 18, 2014 at 02:24 AM
Try this to normalize your angle to 0 to 360:
float angle = Mathf.Atan2 (dir.x, dir.z) * Mathf.Rad2Deg;
if (angle < 0.0f) {
angle = 360.0 - angle;
}
Good thinking but I it's jumping all around now,
Angle is 2.2 & Index is 0 in one from and
Angle is 368 & Index 17 in the next
then it works for 7 images (<180 deg) before the Array index goes out of range. I really appreciate your help on this RB
~ be
Hey
I tried making the Index Absolute and it seems to have helped:
index = $$anonymous$$athf.Abs (index);
Debug.Log ("index = " + index);
if (index <= textures.Length) {
GetComponent<Renderer> ().material.mainTexture = textures [index];
}
A L $$anonymous$$ O S T ... Thanks RB !
Your answer
Follow this Question
Related Questions
Rotating a Rotated GUI Texture 0 Answers
Rotating Textures 0 Answers
sphere texture rotation around pivot (eye) 1 Answer