Created
February 5, 2019 08:44
-
-
Save unitycoder/a9358cf272c90497668865104f0797c2 to your computer and use it in GitHub Desktop.
parallax effect in editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// parallax effect in editor | |
// https://old.reddit.com/r/Unity3D/comments/an90qs/this_little_tool_made_me_so_happy/ | |
using UnityEngine; | |
using Sirenix.OdinInspector; //this requires Odin to use the buttons, but you could write a custom editor to do the same thing. I'm not going to teach you how to do that, though. :) | |
using UnityEditor; | |
[ExecuteInEditMode] //run this script in the editor. | |
public class ParallaxObject : MonoBehaviour { | |
bool emulateGameCamera = false; | |
[Button, HideIf("emulateGameCamera")] //hide this button if we are using the game camera | |
void UseEditorCamera() { | |
EditorPrefs.SetBool("emulateGameCamera", true); //if we want to use the editor camera, set the EDITOR pref to true | |
} | |
[Button, ShowIf("emulateGameCamera")] //show this button if we are using the game camera | |
void StopUsingEditorCamera() { | |
EditorPrefs.SetBool("emulateGameCamera", false); //we're done using the editor camera, so set the editor pref to false. | |
} | |
[SerializeField, Range(-1f, 1f), PropertyTooltip("Horizontal speed of the object. 0 means it doesn't move at all. 1 is further away, -1 is closer.")] | |
float speedX; | |
[SerializeField, Range(-1f, 1f), PropertyTooltip("Vertical speed of the object. 0 means it doesn't move at all. 1 is further away, -1 is closer.")] | |
float speedY; | |
[SerializeField, PropertyTooltip("The root position of the object. All movement is based around this location.")] | |
Vector3 originalPosition; | |
Vector2 heading; | |
private void Start() { | |
originalPosition = transform.position; //grab the objects position at start, in case it wasn't saved for some reason. | |
} | |
private void Update() { | |
//if we're in game, find the heading to the camera. Multiply the heading by our speed, then add it to our original position. This gives us the parallaxed position. | |
//NOTE you will have to replace Game.instance.proCamera.transform.position with the position of your camera. if you're not sure how to do this, try Camera.main.transform.position | |
if(Application.isPlaying) { | |
heading = Game.instance.proCamera.transform.position - originalPosition; | |
transform.position = new Vector3(originalPosition.x + (heading.x * speedX), originalPosition.y + (heading.y * speedY), 0f); | |
return; | |
} | |
#if UNITY_EDITOR | |
//this code only executes in the editor | |
//if our camera state changed in the editor, change it locally. Then set our position to our original position. | |
if(emulateGameCamera != EditorPrefs.GetBool("emulateGameCamera", false)) { | |
emulateGameCamera = EditorPrefs.GetBool("emulateGameCamera", false); | |
transform.position = originalPosition; | |
} | |
//if we are not using the scene camera in the editor, keep the position at the original position. | |
if(!emulateGameCamera) { | |
originalPosition = transform.position; | |
} else { //otherwise, if our scene view is active, do what we normally do in game, but with the scene camera instead of the game camera. | |
if(Camera.current) { | |
heading = Camera.current.transform.position - originalPosition; | |
transform.position = new Vector3(originalPosition.x + (heading.x * speedX), originalPosition.y + (heading.y * speedY), 0f); | |
} | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment