In my pursuit for getting all my errant scripts in to proper classes I went to some of the basic operations. In this one, it was the parallax scrolling standard for 2D platformers and fighting games. The class takes two background objects and scrolls their texture material depending on the players movement. It requires a velocity property from the character, and the class method should be called from the player class, or some other attached script that has a regularly updated reference to the players actual velocity.
For anyone new to parallax scrolling in unity, all you do is setup two game objects such as cubes or quads away from your main scene, separate them by one on the Z axis, set their shaders to unlit transparent, and then place a camera of the same aspect ratio as your main camera, and then set the camera the same distance from the cubes as the distance from you main camera to main scene.Then, set up public game object properties, drag the game objects to the reference in the UI, then in the main script take those game objects and pass them to the ParallaxCamera's ParallaxScroll method along with the player velocity.
using UnityEngine;
using System.Collections;
public class ParallaxCamera{
private float moveParallax = 0;
private float speed = 0;
void setScrollSpeed(float setSpeed)
{
speed = setSpeed;
}
void ParallaxScroll(GameObject firstBackground, GameObject secondBackground, float velocitySpeed)
{
if(velocitySpeed != 0)
{
if(Input.GetAxis("Horizontal")>0){
firstBackground.renderer.material.mainTextureOffset = new Vector2((moveParallax * speed)%1, 0f);
secondBackground.renderer.material.mainTextureOffset = new Vector2((moveParallax * (speed/5.0f))%1, 0f);
moveParallax+=.0005f;
}else if(Input.GetAxis("Horizontal")<0 p=""> {
firstBackground.renderer.material.mainTextureOffset = new Vector2(((speed*moveParallax)%1), 0f);
secondBackground.renderer.material.mainTextureOffset = new Vector2((moveParallax * (speed/5.0f))%1, 0f);
moveParallax-=.0005f;
}else{
return;
}
}
}
}
}0>
Again if anyone needs the working script opposed to this reference, there's one in my bitbucket repo, as usual.
No comments:
Post a Comment