Guest User

Movement/Attacking/Knockback Code

a guest
Nov 24th, 2015
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player1Control : MonoBehaviour {
  5.    
  6.     public LayerMask playerMask;
  7.     public GameObject target;
  8.     public string currentAction = "Idle";
  9.     public float speed = 12;
  10.     public float jump = 7;
  11.     public int damage=0;
  12.  
  13.     public bool isGrounded = false;
  14.     public bool extraJump = false;
  15.     public bool facingRight = true;
  16.     public Transform myTrans;
  17.     public Transform targTrans;
  18.     public Transform ground;
  19.     public Rigidbody myBody;
  20.     public Rigidbody targBody;
  21.  
  22.  
  23.     void Start() {
  24.         myBody = this.GetComponent<Rigidbody>();
  25.         myTrans = this.transform;
  26.         targBody = target.GetComponent<Rigidbody>();
  27.         targTrans = target.transform;
  28.         ground = GameObject.Find(this.name + "/ground").transform;
  29.     }
  30.  
  31.     void Update() {
  32.         if(damage<0) damage=0;
  33.         if(damage>999) damage=999;
  34.     }
  35.  
  36.     void FixedUpdate() {
  37.         isGrounded = Physics.Linecast (myTrans.position, ground.position, playerMask);
  38.         if(isGrounded) extraJump=false;
  39.        
  40.         if(currentAction=="Idle"||currentAction=="Moving"||currentAction=="Jumping") Move(Input.GetAxisRaw("MoveDigital P1"));
  41.         if(Mathf.Abs(Input.GetAxis("Horizontal P1"))>0.3&&Mathf.Abs(Input.GetAxis("Horizontal P1"))<0.7) { MoveSlow(Input.GetAxisRaw("Horizontal P1")); }
  42.         if(Mathf.Abs(Input.GetAxis("Horizontal P1"))>=0.7) { Move(Input.GetAxisRaw("Horizontal P1")); }
  43.         if(Input.GetButtonDown("Jump P1")) {
  44.             Jump();
  45.         }
  46.         if(!isGrounded) { myBody.velocity += Vector3.down; currentAction="Jumping"; }
  47.  
  48.         if(isGrounded&&!Input.GetButtonDown("Jump P1")&&Mathf.Abs(Input.GetAxis("Horizontal P1"))<0.3&&Mathf.Abs(Input.GetAxisRaw("MoveDigital P1"))<0.3&&currentAction!="Neutral A"&&Input.GetAxisRaw("VerticalDigital P1")==0) {
  49.             currentAction="Idle";
  50.         }
  51.  
  52.         if(isGrounded&&Input.GetAxisRaw("VerticalDigital P1")==-1) { currentAction="Crouch Smash"; }
  53.         if(isGrounded&&Input.GetAxisRaw("VerticalDigital P1")==1)  { currentAction="20XX"; }
  54.  
  55. //      print(Input.GetAxisRaw("VerticalDigital P1"));
  56.         if(isGrounded&&Input.GetAxis("Vertical P1")<0.7&&Input.GetAxis("Vertical P1")>0.3) currentAction="NoJohns";
  57.         if(isGrounded&&Input.GetAxis("Vertical P1")>=0.7) currentAction="20XX";
  58.         if(isGrounded&&Input.GetAxis("Vertical P1")>-0.7&&Input.GetAxis("Vertical P1")<-0.3) currentAction="Crouching";
  59.         if(isGrounded&&Input.GetAxis("Vertical P1")<=-0.7) currentAction="Crouch Smash";
  60.  
  61.  
  62.         if(currentAction=="Idle"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Neutral"); }
  63.         if(currentAction=="Moving Slow"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Side"); }
  64.         if(currentAction=="NoJohns"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Up"); }
  65.         if(currentAction=="Crouching"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Down"); }
  66.  
  67.         if(currentAction=="Moving"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Side"); }
  68.         if(currentAction=="20XX"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Up"); }
  69.         if(currentAction=="Crouch Smash"&&Input.GetButtonDown("Normal Attack P1")) { Normal("Down"); }
  70.     }
  71.  
  72.     public void Move(float hInput) {
  73.         currentAction="Moving";
  74.         if(hInput>0) facingRight=true;
  75.         if(hInput<0) facingRight=false;
  76.         Vector3 hspd = myBody.velocity;
  77.         hspd.x = hInput*speed;
  78.         myBody.velocity = hspd;
  79.     }
  80.     public void MoveSlow(float hInput) {
  81.         currentAction="Moving Slow";
  82.         if(hInput>0) facingRight=true;
  83.         if(hInput<0) facingRight=false;
  84.         Vector3 hspd = myBody.velocity;
  85.         hspd.x = (hInput*speed)/4;
  86.         myBody.velocity = hspd;
  87.     }
  88.     public void Jump() {
  89.         if(isGrounded) { myBody.velocity = Vector3.zero; myBody.AddForce(transform.up*jump*5, ForceMode.Impulse); extraJump=true; return; }
  90.         if(extraJump) { myBody.velocity = Vector3.zero; myBody.AddForce(transform.up*jump*5, ForceMode.Impulse); extraJump=false; return; }
  91.     }
  92.     public void Damage(string attack, string direction) {
  93.         if(attack=="Normal") {
  94.             if(direction=="neutral") {
  95.                 targBody.velocity = Vector3.zero;
  96.                 if(facingRight) {
  97.                     Vector3 newVel = new Vector3(target.GetComponent<Player1Control>().damage*6, target.GetComponent<Player1Control>().damage*2, 0);
  98.                     targBody.AddForce(newVel, ForceMode.Acceleration);
  99.                     print("Stuff");
  100.                 }
  101.                 if(!facingRight) {
  102.                     targBody.AddForce(transform.up*-1*target.GetComponent<Player1Control>().damage/16, ForceMode.Impulse);
  103.                     targBody.AddForce(transform.right*-1*target.GetComponent<Player1Control>().damage, ForceMode.Impulse);
  104.                     print("Other Stuff");
  105.                 }
  106.                 target.GetComponent<Player1Control>().damage+=3;
  107.             }
  108.         }
  109.     }
  110.     public void Normal(string direction) {
  111.         float distance = Vector3.Distance(target.transform.position, transform.position);
  112.  
  113.         if(direction=="Neutral") {
  114.             if (distance < 1.3f) {
  115.                 if (distance > 0 && ((facingRight==true&&targTrans.position.x>myTrans.position.x)||(facingRight==false&&targTrans.position.x<myTrans.position.x))) {
  116.                     print("Neutral A");
  117.                     Damage("Normal","neutral");
  118.                 }
  119.             }
  120.         }
  121.        
  122.         else if(direction=="Side") {
  123.             if (distance < 2f) {
  124.                 if (distance > 0 && ((facingRight==true&&targTrans.position.x>myTrans.position.x)||(facingRight==false&&targTrans.position.x<myTrans.position.x))) {
  125.                     print("Side A");
  126.                     target.GetComponent<Player1Control>().damage+=2;
  127.                 }
  128.             }
  129.         }
  130.        
  131.         else if(direction=="Up") {
  132.             if (distance < 1.3f) {
  133.                 if (distance > 0 && ((facingRight==true&&targTrans.position.x>myTrans.position.x)||(facingRight==false&&targTrans.position.x<myTrans.position.x))) {
  134.                     print("Up A");
  135.                     target.GetComponent<Player1Control>().damage+=3;
  136.                 }
  137.             }
  138.         }
  139.        
  140.         else if(direction=="Down") {
  141.             if (distance < 1.6f) {
  142.                 if (distance > 0 && ((facingRight==true&&targTrans.position.x>myTrans.position.x)||(facingRight==false&&targTrans.position.x<myTrans.position.x))) {
  143.                     print("Down A");
  144.                     target.GetComponent<Player1Control>().damage+=4;
  145.                 }
  146.             }
  147.         }
  148.  
  149.     }
  150. }
RAW Paste Data