0

I am making a game that has a feature whereby 2D and 3D elements are placed in a world together. here you have an idea of the kind of mix that can be seen in my game.

enter image description here

I acheive this by placing my camera inside a gameObject called cameraHolder and simply rotating the cameraHolder along its z axis. This way the camera, which is attached to the holder, is always at a 41 degree angle pointing at the ground. Now you can imagine that I need to rotate the sprite to match 2 values. first) the z axis of the cameraHolder and second, I must place an angle on the sprite in oder for it to not be "lying" on the ground but rather facing the camera at all times. This is where the problems arrise.

enter image description here

Here you have an example of the rotation of the camera (The sprite of the chracter rotates on the z axis to match the cameraHolder and maintains it's rotation on the x axis so it always faces the camera.)

enter image description here

Now you'd think everything is great as this is actually the look and feel that I want. However, sporadically, and I cannot fathom what causes it, all the sprites will revert to being lying down along the floor and thus breaking the illusion. When trying to fix this I can realise that I dont really understand why it was working in the first place and perhaps if I understand that I can fix this in a permanent way.

the script that organises the rotation of the sprite is as follows.

     public class FaceCamera : MonoBehaviour {

     public Transform target;

     void Start()
     {
         target = GameObject.Find("MainCamera").GetComponent<Transform>();
     }

     void Update()
     {

         if (target != null)
         {
             this.transform.rotation = target.rotation;
         }
     }
 }

I am aware that for optimizations sake I should add a Mathf.Approximately and compare the two values and only do it if it has a changed value. But this is for testing purposes.

So my question is. Why does setting my sprites rotation to be equal to the cameras rotation work in the first place. The cameras rotation in eulerangles is (-41,0,0)?

Things I tried:

  • Using rotation/position constraints components
  • Setting a script that detects when it fails and fixes the issue (ugly I know but wanted to see if it works, doesnt)
  • Tried applying eulerangle rotations directly, this causes weird behaviour when rotating the screen, think it could be gimbal lock.

references:

https://answers.unity.com/questions/48609/whats-the-difference-between-rotation-eulerangles.html

UPDATE 1

Transform cam;
[SerializeField] float threshold;

public static bool FastApproximately(float a, float b, float threshold)
{
    return ((a - b) < 0 ? ((a - b) * -1) : (a - b)) <= threshold;
}

void Start()
{
    cam = Camera.main.transform;
    transform.rotation = cam.rotation;
}

void OnWillRenderObject()
{
    if (!FastApproximately(cam.rotation.x, transform.rotation.x, 0.0001f))
    {
        transform.rotation = cam.rotation;
    }
}
SteenPetersen
  • 188
  • 2
  • 17
  • You have a plane that represents the camera view. If you want your sprites to be parallel to this plane (just at some distance), they need to have the same rotation as your camera and that's what you do. (For optimization I'd probably update the sprites position if the camera rotation changes.) For the constraints, what did you try? Locking x and y should work. With that, you only need to update their z value (like with the camera holder object). – Gunnar B. Aug 03 '18 at 12:11
  • Update 1 shows some code that I put on my enemies that basically fixes the problem for them - It does submit many many calls to set their quaternions, as I understand it however, this should not be a very heavy operation? My problem now is that for some rreason a child object in my player, holding the sprites, gets set to have exactly the opposite rot as the camera it is following. I can reproduce that by unparenting then partenting. Ctrl + shift + F in visual studio to look for instances where that could be happening is not showing me anything. Dont know why it is or how to figure it out. – SteenPetersen Aug 03 '18 at 13:30

0 Answers0