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 P1weapon : MonoBehaviour {
  4. public float pushMagnitude = 500;
  5. void Start () {
  6. renderer.enabled = false;
  7. }
  8. void Update (){
  9. if (Input.GetKeyDown (KeyCode.KeypadEnter)) {
  10. // show
  11. renderer.enabled = true;
  12. collider2D.enabled = true;
  13. }
  14. if (Input.GetKeyUp (KeyCode.KeypadEnter)){
  15. renderer.enabled = false;
  16. collider2D.enabled = false;
  17. }
  18. }
  19. void OnCollisionEnter2D (Collision2D other){
  20. Vector2 ab = new Vector2 (0, 0);
  21. //pushmagntiude
  22. if (other.gameObject.name == "Cube2") {
  23. //Get a and b
  24. Vector2 a;
  25. Vector2 b;
  26. a = transform.position;
  27. b = other.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. other.gameObject.rigidbody2D.AddForce (ab, ForceMode2D.Force);
  42. other.gameObject.GetComponent<player2_controls>().Pushed();
  43. }
  44. }
  45. // Update is called once per frame
  46. }