Forum rules - please read before posting.

Camera following mouse cursor and 2D parallax effect

Hello and happy new year!

I'm making a 2D game and I'd like the camera to follow the mouse cursor to create a parallax.
The best example of what I'm trying to achieve is the game Tsioque:
https://youtube.com/watch?v=U0qlCv6TdTQ
I like that subtle parallax effect that gives a sense of depth even in scenes where the camera is "static".

I've followed the explanations from that discussion:
https://adventurecreator.org/forum/discussion/7471/2d-camera-following-mouse-cursor

The following code will cause a GameObject to follow your cursor in 2D space:
http://pasteall.org/944549/csharp

Paste it into a C# script named FollowCursor.cs, and add it to a GameObject in your scene (you may want to also attach a sprite so that you can visualise it while testing).

Then in your 2D GameCamera, uncheck Target is Player? and assign this new GameObject as the new Target field that appears beneath.

Strangely, the code in the discussion didn't work for me so I looked for another one on the internet and found this one which perfectly works:

using UnityEngine;
using System.Collections;

namespace AC
{ 
    public class FollowCursor : MonoBehaviour
        {
        private void Update ()
            {
            Vector3 FollowMouse = Input.mousePosition;
            FollowMouse.z = 10f; // Set this to be the distance you want the object to be placed in front of the camera.
            this.transform.position = Camera.main.ScreenToWorldPoint(FollowMouse);
            }
        }
}

So it works, my camera follows the mouse and creates the parallax effect.
But the movement is far too brutal: it only occurs in the center of the screen.
When I play the game, if I put the cursor in the middle of the screen and slightly move it to the left, the camera immediately moves to the right (and won't move anymore if the cursor goes farther on the left). Then, if I move the cursor slightly to the right, the camera quickly goes to the right (and won't move anymore if the cursor goes farther on the right).
Here is a video showing my problem:
https://youtu.be/bXFtcOCurfc

In Tsioque, the camera is smoother: it starts when the cursor is on the left border of the screen and ends when it reaches the right border of the screen.
I've no idea how to create that kind of movement. If anyone has an idea, I'm interested!
Sorry for the big post and thank you for reading me!

Technical info:
I'm using Unity 2018.3.0f2 and AC v1.65.2

Comments

  • You should be able to get help with this on the general Unity forums. This isn't specifically related to AC, but just involves tweaking of the non-AC script you already have.

    However, I will look to see what's possible on the AC side of things to allow for this more easily.

  • OK!
    Thanks a lot for looking what's possible in AC!
    It's a cool effect and I loved how it looked in Tsioque.

    Meanwhile I'll look on the general Unity forum if I find help and I'll update this post when I find a solution.

  • Actually, this should be possible in AC with some tweaks. Look out for the upcoming update, ETA next week.

  • That's wonderful!
    Thank you so much! I'm downloading it right now.

  • Just a little feedback: I've tested it and it's just perfect!
    It looks so good and so smooth. Thank you!

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.