You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB

9 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. public class P2_weapon : MonoBehaviour {
  4. public float pushMagnitude = 500;
  5. void Start () {
  6. renderer.enabled = false;
  7. }
  8. void Update (){
  9. if (Input.GetKeyDown (KeyCode.Space)) {
  10. // show
  11. renderer.enabled = true;
  12. collider2D.enabled = true;
  13. }
  14. if (Input.GetKeyUp (KeyCode.Space)){
  15. renderer.enabled = false;
  16. collider2D.enabled = false;
  17. }
  18. }
  19. void OnCollisionEnter2D (Collision2D col){
  20. Vector2 ab = new Vector2 (0, 0);
  21. //pushmagntiude
  22. if (col.gameObject.name == "Cube") {
  23. //Get a and b
  24. Vector2 a;
  25. Vector2 b;
  26. a = transform.position;
  27. b = col.transform.position;
  28. //Calculate AB->
  29. ab = b - a;
  30. Debug.Log (ab);
  31. //Modify to add height
  32. ab = new Vector2 (ab.x,ab.y +0.8f);
  33. Debug.Log (ab);
  34. //Normalise AB
  35. ab.Normalize();
  36. Debug.Log (ab);
  37. //Multiple AB by push magnitude
  38. ab = ab * pushMagnitude;
  39. Debug.Log (ab);
  40. //Push opponent by AB * magnitude
  41. col.gameObject.rigidbody2D.AddForce (ab, ForceMode2D.Force);
  42. col.gameObject.GetComponent<PlayerControler>().Pushed();
  43. }
  44. }
  45. // Update is called once per frame
  46. }