2D Parallax Mouse Movement
I want to create a 2D parallax mouse movement in my project. It's like the 2nd example shown here. The movement should only works in the x-direction.
The game is an escape-the-room game in first person. You can navigate only with your mouse.
I need one to three layers: background, shadows(additional) and foreground.
I'm really new in Unity and coding. So: are there any examples to learn (easily) or has anybody a hint (or maybe a solution)?
Answer by DinoBike · Oct 29, 2017 at 08:12 AM
@herrgrab Put this script on each UI object you want to parrallax and edit there modifier value accordingly:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoveRelative2Mouse : MonoBehaviour {
public Vector3 pz;
public Vector3 StartPos;
public int moveModifier;
// Use this for initialization
void Start ()
{
StartPos = transform.position;
}
// Update is called once per frame
void Update ()
{
Vector3 pz = Camera.main.ScreenToViewportPoint(Input.mousePosition);
pz.z = 0;
gameObject.transform.position = pz;
//Debug.Log("Mouse Position: " + pz);
transform.position = new Vector3(StartPos.x + (pz.x * moveModifier), StartPos.y + (pz.y * moveModifier), 0);
//move based on the starting position and its modified value.
}
}
Your answer
Follow this Question
Related Questions
Inverted parallax 0 Answers
Parallax moving objects X parallax with cameras 0 Answers
2D Parallax with zoom 0 Answers
Box Collider 2d with Rigid Body 2d, strange behavior. 0 Answers
2D Outline Shader working in Node Preview But not when rendering 0 Answers