using UnityEngine; using System.Collections; public class P1weapon : MonoBehaviour { public float pushMagnitude = 500; void Start () { renderer.enabled = false; } void Update (){ if (Input.GetKeyDown (KeyCode.KeypadEnter)) { // show renderer.enabled = true; collider2D.enabled = true; } if (Input.GetKeyUp (KeyCode.KeypadEnter)){ renderer.enabled = false; collider2D.enabled = false; } } void OnCollisionEnter2D (Collision2D other){ Vector2 ab = new Vector2 (0, 0); //pushmagntiude if (other.gameObject.name == "Cube2") { //Get a and b Vector2 a; Vector2 b; a = transform.position; b = other.transform.position; //Calculate AB-> ab = b - a; Debug.Log (ab); //Modify to add height ab = new Vector2 (ab.x,ab.y +0.8f); Debug.Log (ab); //Normalise AB ab.Normalize(); Debug.Log (ab); //Multiple AB by push magnitude ab = ab * pushMagnitude; Debug.Log (ab); //Push opponent by AB * magnitude other.gameObject.rigidbody2D.AddForce (ab, ForceMode2D.Force); other.gameObject.GetComponent().Pushed(); } } // Update is called once per frame }