Advertisement
Jon50

Sprite Colors

Jan 18th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerSprite : MonoBehaviour
  4. {
  5.     [SerializeField] float movementSpeed = 5f;
  6.     [SerializeField] float jumpSpeed = 5f;
  7.  
  8.     //Use this this set all the color you need
  9.     [SerializeField] Color[] myColors = new Color[]{};
  10.  
  11.     private SpriteRenderer spriteRenderer;
  12.  
  13.     Rigidbody2D myRigidbody;
  14.     Animator myAnimator;
  15.  
  16.     //Movement string refs
  17.     const string GET_HORIZONTAL_AXIS = "Horizontal";
  18.     const string GET_JUMP = "Jump";
  19.     const string RUNNING_ANIM = "IsPlayerRunning";
  20.     const string JUMPPING_ANIM = "IsJumpping";
  21.  
  22.     //Input string refs
  23.     const string INPUT_FIRE1 = "Fire1";
  24.     const string INPUT_FIRE2 = "Fire2";
  25.     const string INPUT_FIRE3 = "Fire3";
  26.     const string INPUT_FIRE4 = "Fire4";
  27.  
  28.     // Start is called before the first frame update
  29.     void Start()
  30.     {
  31.         myRigidbody = GetComponent<Rigidbody2D>();
  32.         myAnimator = GetComponent<Animator>();
  33.         spriteRenderer = GetComponent<SpriteRenderer>();
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         Run();
  40.         Jump();
  41.         FlipSprite();
  42.         ColourChange();
  43.     }
  44.  
  45.     private void Run()
  46.     {
  47.         float controlXAxis = CrossPlatformInputManager.GetAxis(GET_HORIZONTAL_AXIS);//Value is between -1 and 1
  48.         Vector2 playerVelocity = new Vector2(controlXAxis * movementSpeed, myRigidbody.velocity.y);
  49.         myRigidbody.velocity = playerVelocity;
  50.  
  51.         bool playerHorizontalMovement = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
  52.         myAnimator.SetBool(RUNNING_ANIM, playerHorizontalMovement);
  53.     }
  54.  
  55.     private void Jump()
  56.     {
  57.         if(CrossPlatformInputManager.GetButtonDown(GET_JUMP))
  58.         {
  59.         Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
  60.         myRigidbody.velocity += jumpVelocityToAdd;
  61.  
  62.         // bool playerVerticalMovement = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
  63.         //myAnimator.SetBool(JUMPPING_ANIM, playerVerticalMovement);
  64.         }
  65.     }
  66.  
  67.     private void FlipSprite()
  68.     {
  69.         bool playerHorizontalMovement = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
  70.         if(playerHorizontalMovement)
  71.         {
  72.             transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f);
  73.         }
  74.     }
  75.  
  76.     private void ColourChange()
  77.     {
  78.         if (CrossPlatformInputManager.GetButtonDown(INPUT_FIRE1))
  79.         {
  80.             spriteRenderer.color = myColors[0];
  81.         }
  82.         else if (CrossPlatformInputManager.GetButtonDown(INPUT_FIRE2))
  83.         {
  84.              spriteRenderer.color = myColors[1];
  85.         }
  86.         else if (CrossPlatformInputManager.GetButtonDown(INPUT_FIRE3))
  87.         {
  88.              spriteRenderer.color = myColors[2];
  89.         }
  90.         else if (CrossPlatformInputManager.GetButtonDown(INPUT_FIRE4))
  91.         {
  92.              spriteRenderer.color = myColors[3];
  93.         }
  94.     }
  95.  
  96. }
  97.  
  98. //-----------------------//
  99. //-----------------------//
  100. //-----------------------//
  101.  
  102. using UnityEngine;
  103.  
  104. public class Platforms : MonoBehaviour
  105. {
  106.     //Use this to set the platform color
  107.     [SerializeField] Color platFormColor = Color.white;
  108.  
  109.     //Tag string ref
  110.     const string PLAYER_TAG = "Player";
  111.  
  112.     private void Awake()
  113.     {
  114.         GetComponent<SpriteRenderer>().color = platFormColor;
  115.     }
  116.  
  117.     private void OnCollisionEnter2D(Collision2D player)
  118.     {
  119.         if (player.gameObject.CompareTag(PLAYER_TAG))
  120.         {
  121.             var playerColor = player.transform.GetComponent<SpriteRenderer>().color;
  122.             if (playerColor == platFormColor)
  123.             {
  124.                 player.transform.SetParent(this.transform);
  125.             }
  126.         }
  127.     }
  128.  
  129.     private void OnCollisionExit2D(Collision2D player)
  130.     {
  131.         if (player.gameObject.CompareTag(PLAYER_TAG))
  132.         {
  133.             player.transform.SetParent(null);
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement