Forum rules - please read before posting.

(2D) Making objects follow camera's position and view size

Hello!

I'm trying to have a static background with some slight parallax effect in my 2D scene. I'm switching between cameras that have different orthographic view size (I do this to zoom in/out) and whenever I'm switching the view size, all the game objects become "bigger". How can I make certain background elements transform and scale in relation to the camera size and movement? I want certain objects to seem very static and I'm having slight problems figuring out how to do this properly. Right now my method is by using an action script, in which I change the object's transform and scale whenever I'm switching between cameras.

Here's a gif without any transform or scale changes when switching between cameras:
https://gfycat.com/beneficialplaincub

Here's a gif with my attempt at making the bg space/galaxy stay at the same position and scale, while making the bg mountains change its scale slightly to add more to the depth of view. And as you can see, any camera movement makes the bg objects look unnatural because it's quite hard to manually make the object's transform follow camera movement:
https://gfycat.com/UnsteadyAbleHarrier

It's easy to calculate the change of scale in relation to camera's view size, for example: switching between camera view size 5 and 2 while sprite has a scale of 1.1.0 (x.y.z) -> (2/5).(2/5).0 creates the right scale for the smaller view size. I'm using the same transition time for both camera and scale change, so if the camera doesn't move at all, you shouldn't be able to see anything weird about the sprite changing scale. But when the camera moves horizontally or vertically even a bit, you can see the sprites move unnaturally.

tl;dr:

How can I make a 2D object change position and scale so that they appear the same size whenever camera's position and size changes in a 2D scene, yet still maintain some slight parallax effect which occurs when main character is moving around? Basically I need to make the object follow camera's movement and change its scale whenever camera's orthographic view size changes, but is there any accurate way to do this within AC?

Thank you for an awesome tool!

Comments

  • The included "Parallax 2D" component can be used to translate sprite objects when the camera moves, giving a parallax effect by having them move at a different rate to the camera. The 2D Demo uses this component for its background and foreground objects. See the Manual's "Parallax 2D" chapter for more on its usage.

    It won't control an object's scale, however - but if you're already worked out the relationship between scale and camera view size then it should just be a matter of attaching a simple script to it, i.e.:

    using UnityEngine;
    using System.Collections;
    
    public class SyncScaleWithOrthographicSize : MonoBehaviour
    {
    
        public float scaleFactor = 0.2f;
    
        private void Update ()
        {
            transform.localScale = Vector3.one * Camera.main.orthographicSize * scaleFactor;
        }
    
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.