Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GunMove : MonoBehaviour {
  6. GameObject bullet;
  7. // Use this for initialization
  8. void Start () {
  9.  
  10. bullet = MakeCircle(20);
  11. }
  12.  
  13. // Update is called once per frame
  14. void Update () {
  15.  
  16.  
  17.  
  18. Vector3 mPos = Input.mousePosition;
  19.  
  20. var pos = Camera.main.WorldToScreenPoint(transform.position);
  21. var dir = Input.mousePosition - pos;
  22. var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  23. gameObject.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  24.  
  25. if(Input.GetKeyDown(KeyCode.Mouse0))
  26. {
  27. shoot();
  28. }
  29.  
  30. }
  31.  
  32. void shoot()
  33. {
  34. var outcome = GameObject.FindGameObjectsWithTag("Shooty");
  35. Vector3 sPos = outcome[0].transform.position;
  36.  
  37. Vector2 aimPos = Input.mousePosition;
  38.  
  39. var pos = Camera.main.WorldToScreenPoint(outcome[0].transform.position);
  40.  
  41. GameObject projectile = Instantiate(bullet,sPos + transform.right*0.25f, transform.rotation, transform.parent);
  42.  
  43. Rigidbody2D rb = projectile.GetComponent<Rigidbody2D>();
  44. rb.AddForce(transform.right * 10,ForceMode2D.Impulse);
  45.  
  46.  
  47.  
  48.  
  49. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  50. Debug.DrawRay(pos, aimPos);
  51.  
  52.  
  53. }
  54.  
  55. public GameObject MakeCircle(int numOfPoints)
  56. {
  57. float angleStep = 360.0f / (float)numOfPoints;
  58. List<Vector3> vertexList = new List<Vector3>();
  59. List<int> triangleList = new List<int>();
  60. Quaternion quaternion = Quaternion.Euler(0.0f, 0.0f, angleStep);
  61. // Make first triangle.
  62. vertexList.Add(new Vector3(0.0f, 0.0f, 0.0f)); // 1. Circle center.
  63. vertexList.Add(new Vector3(0.0f, 0.5f, 0.0f)); // 2. First vertex on circle outline (radius = 0.5f)
  64. vertexList.Add(quaternion * vertexList[1]); // 3. First vertex on circle outline rotated by angle)
  65. // Add triangle indices.
  66. triangleList.Add(0);
  67. triangleList.Add(1);
  68. triangleList.Add(2);
  69. for (int i = 0; i < numOfPoints - 1; i++)
  70. {
  71. triangleList.Add(0); // Index of circle center.
  72. triangleList.Add(vertexList.Count - 1);
  73. triangleList.Add(vertexList.Count);
  74. vertexList.Add(quaternion * vertexList[vertexList.Count - 1]);
  75. }
  76. Mesh mesh = new Mesh();
  77. mesh.vertices = vertexList.ToArray();
  78. mesh.triangles = triangleList.ToArray();
  79.  
  80. GameObject go = new GameObject("Test");
  81. Debug.Log("Another one wtf");
  82. Material material = Resources.Load("Materials/Damn", typeof(Material)) as Material;
  83.  
  84. Vector3 vec = new Vector3();
  85. vec.Set(0.25f, 0.25f, 0.25f);
  86. go.transform.localScale = vec;
  87.  
  88. PhysicsMaterial2D physicsBoy = new PhysicsMaterial2D();
  89. physicsBoy.friction = 0.4f;
  90. physicsBoy.bounciness = 0.4f;
  91. CircleCollider2D collider2d = go.AddComponent<CircleCollider2D>();
  92.  
  93. collider2d.sharedMaterial = physicsBoy;
  94.  
  95. Rigidbody2D rbody = go.AddComponent<Rigidbody2D>();
  96. rbody.simulated = true;
  97. rbody.mass = 1;
  98. rbody.drag = 0;
  99. rbody.angularDrag = 0.05f;
  100. rbody.gravityScale = 1;
  101. rbody.collisionDetectionMode = CollisionDetectionMode2D.Discrete;
  102. rbody.sleepMode = RigidbodySleepMode2D.StartAwake;
  103. rbody.interpolation = RigidbodyInterpolation2D.None;
  104.  
  105.  
  106. MeshFilter meshFilter = go.AddComponent<MeshFilter>();
  107. meshFilter.mesh = mesh;
  108. MeshRenderer spriteRenderer = go.AddComponent<MeshRenderer>();
  109. spriteRenderer.material = material;
  110. return go;
  111. }
  112.  
  113.  
  114.  
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement