For Parallaxing the background, we need to follow few steps:
1. The type of image using for parallax must be of type texture
2. Set it's wrap mode to be repeated.
3. Assign the image to a quad
4. set shader as Uint/Texture
5. Assign the script BackgroundScroller.cs to quad
using UnityEngine;
using System.Collections;
public class BackGroundScroller : MonoBehaviour {
public static BackGroundScroller current;
float pos;
public float speed=0;
// Use this for initialization
void Start () {
current = this;
}
// Update is called once per frame
public void go (string dir) {
pos += speed;
if (pos > 1f)
pos -= 1f;
if(dir == "Right")
this.GetComponent<Renderer> ().material.mainTextureOffset = new Vector2 (pos, 0);
else
this.GetComponent<Renderer> ().material.mainTextureOffset = new Vector2 (-pos, 0);
}
}
7.Create another script Movement.cs and assign it to the camera.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float speed=2f;
//public GameObject target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float _horizontal=Input.GetAxis("Horizontal");
if (_horizontal < 0) {
this.transform.Translate (Vector2.left * Time.deltaTime * speed);
BackGroundScroller.current.go("Left");
} else if (_horizontal > 0) {
this.transform.Translate (-Vector2.left * Time.deltaTime * speed);
BackGroundScroller.current.go("Right");
}
}
}
8.Made Quad as the child of Main Camera.
Now As you go Right and Left via arrow keys you can see parallax being happening with the image.
Note: For Reference Please finds the attach unity package with the blog.
Thanks, Please leave the comments if you have any query.
0 Comment(s)