Guest User

C# sprite Movement

a guest
Feb 18th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Main : MonoBehaviour {
  5.  
  6.         public Sprite [] Ninjawalk;
  7.         public float Moveframeindex = 1.0f;
  8.         public float CharPositionXInBlocks;
  9.         public float CharPositionYInBlocks;
  10.                
  11.         private int spriteindex;
  12.         private Vector3 CharPosition;
  13.         private bool MovesetD = true;
  14.         private bool MovesetA = true;
  15.         private bool MovesetW = true;
  16.         private bool MovesetS = true;
  17.         private int buttonlock = 0;
  18.    
  19.         // Use this for initialization
  20.         void Start () {
  21.        
  22.        
  23.         }
  24.        
  25.         // Update is called once per frame
  26.        
  27.         void Update () {
  28.        
  29.         //TODO Paste to notes ( Handy script to determin and designate screen size )
  30.         //float mousePosInBlocks = Input.mousePosition.x / Screen.width;
  31.         //print (mousePosInBlocks);
  32.                
  33.             //Sprite managment
  34.             spriteindex = (int) Moveframeindex -1;     
  35.             CharPositionXInBlocks = this.transform.position.x ;
  36.             CharPositionYInBlocks = this.transform.position.y ;
  37.            
  38.             //Movement controls
  39.             //TODO Move in all directions AND jump and also make idle depending on last facing direction
  40.    
  41.         // IDLE
  42.        
  43.             //if (buttonlock  == 0) { //TODO if buttonlock equals idle right and idle left as well as idle up and down
  44.                 //Moveframeindex = 1f;
  45.             //  this.GetComponent   <SpriteRenderer>().sprite = Ninjawalk [spriteindex];
  46.             //}
  47.            
  48.             if (Input.GetKeyUp(KeyCode.D)){  //---------------------------D
  49.             MovesetD = true;
  50.             buttonlock  = 0;
  51.             }
  52.             if (Input.GetKey(KeyCode.D)) {
  53.                 if (buttonlock  == 0) {
  54.                     buttonlock = 1;
  55.                 } else {
  56.                     if (buttonlock == 1) {
  57.                     if (MovesetD == true) {
  58.                         Moveframeindex = 7f;
  59.                         MovesetD = false;
  60.                     }
  61.                     Moveright();                       
  62.                     }
  63.                 }
  64.             }
  65.            
  66.             if (Input.GetKeyUp(KeyCode.A)){  //---------------------------A
  67.                 MovesetA = true;
  68.                 buttonlock  = 0;
  69.             }
  70.             if (Input.GetKey(KeyCode.A)) {
  71.                 if (buttonlock  == 0) {
  72.                     buttonlock = 2;
  73.                 } else {
  74.                     if (buttonlock == 2) {
  75.                         if (MovesetA == true) {
  76.                             Moveframeindex = 11f;
  77.                             MovesetA = false;
  78.                         }
  79.                         Moveleft();                    
  80.                     }
  81.                 }
  82.             }
  83.            
  84.             if (Input.GetKeyUp(KeyCode.W)){  //---------------------------W
  85.                 MovesetW = true;
  86.                 buttonlock  = 0;
  87.             }
  88.             if (Input.GetKey(KeyCode.W)) {
  89.                 if (buttonlock  == 0) {
  90.                     buttonlock = 3;
  91.                 } else {
  92.                     if (buttonlock == 3) {
  93.                         if (MovesetW == true) {
  94.                             Moveframeindex = 15f;
  95.                             MovesetW = false;
  96.                         }
  97.                         Moveup();                      
  98.                     }
  99.                 }
  100.             }
  101.             if (Input.GetKeyUp(KeyCode.S)){  //---------------------------S
  102.                 MovesetS = true;
  103.                 buttonlock  = 0;
  104.             }
  105.             if (Input.GetKey(KeyCode.S)) {
  106.                 if (buttonlock  == 0) {
  107.                     buttonlock = 4;
  108.                 } else {
  109.                     if (buttonlock == 4) {
  110.                         if (MovesetS == true) {
  111.                             Moveframeindex = 19f;
  112.                             MovesetS = false;
  113.                         }
  114.                         Movedown();                    
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.                
  120.         void Moveright () {
  121.  
  122.             Moveframeindex += 0.12f;       
  123.             if (Ninjawalk [spriteindex]) {
  124.             this.GetComponent   <SpriteRenderer>().sprite = Ninjawalk [spriteindex];
  125.             CharPositionXInBlocks += 0.05f;
  126.             CharPosition = new Vector3 (CharPositionXInBlocks, this.transform.position.y, 0f); 
  127.             CharPosition.x = Mathf.Clamp(CharPosition.x, -3.60f, 3.65f);   
  128.             this.transform.position = CharPosition;
  129.             }
  130.             if (Moveframeindex >= 10.9f) {
  131.             Moveframeindex = 7f;
  132.             }
  133.         }
  134.        
  135.         void Moveleft () {
  136.             Moveframeindex += 0.12f;       
  137.             if (Ninjawalk [spriteindex]) {
  138.             this.GetComponent   <SpriteRenderer>().sprite = Ninjawalk [spriteindex];
  139.             CharPositionXInBlocks -= 0.05f;
  140.             CharPosition = new Vector3 (CharPositionXInBlocks, this.transform.position.y, 0f); 
  141.             CharPosition.x = Mathf.Clamp(CharPosition.x, -3.60f, 3.65f);   
  142.             this.transform.position = CharPosition;        
  143.             }
  144.             if (Moveframeindex >= 14.9f) {
  145.             Moveframeindex = 12f;
  146.             }
  147.         }
  148.        
  149.         void Moveup () {
  150.             Moveframeindex += 0.12f;       
  151.             if (Ninjawalk [spriteindex]) {
  152.             this.GetComponent   <SpriteRenderer>().sprite = Ninjawalk [spriteindex];
  153.             CharPositionYInBlocks += 0.05f;
  154.             CharPosition = new Vector3 (this.transform.position.x, CharPositionYInBlocks, 0f); 
  155.             CharPosition.y = Mathf.Clamp(CharPosition.y, -2.60f, 2.6f);
  156.             this.transform.position = CharPosition;        
  157.             }
  158.             if (Moveframeindex >= 18.9f) {
  159.             Moveframeindex = 15f;
  160.             }
  161.         }
  162.        
  163.         void Movedown () {
  164.             Moveframeindex += 0.12f;       
  165.             if (Ninjawalk [spriteindex]) {
  166.             this.GetComponent   <SpriteRenderer>().sprite = Ninjawalk [spriteindex];
  167.             CharPositionYInBlocks -= 0.05f;
  168.             CharPosition = new Vector3 (this.transform.position.x, CharPositionYInBlocks, 0f); 
  169.             CharPosition.y = Mathf.Clamp(CharPosition.y, -2.6f, 2.6f); 
  170.             this.transform.position = CharPosition;        
  171.             }
  172.             if (Moveframeindex >= 22.9f) {
  173.             Moveframeindex = 19f;
  174.             }
  175.         }
  176. }
Add Comment
Please, Sign In to add comment