Advertisement
Guest User

Untitled

a guest
Jun 14th, 2019
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController_Tank : MonoBehaviour
  6. {
  7.     public Rigidbody2D rb;
  8.     public GameObject PlayerTank;
  9.  
  10.     private Vector3 mousePos;
  11.  
  12.     public float moveSpeed = 5f;
  13.     public float speedMultiplicator = 1f;
  14.  
  15.     public GameObject GunHolder_1;
  16.     public GameObject GunHolder_2;
  17.     public GameObject GunHolder_3;
  18.     public GameObject GunHolder_4;
  19.  
  20.     private GameObject GunBarrel_1_Pivot;
  21.     private GameObject GunBarrel_2_Pivot;
  22.     private GameObject GunBarrel_3_Pivot;
  23.     private GameObject GunBarrel_4_Pivot;
  24.  
  25.     void GetGunHolders()
  26.     {
  27.         if (GunHolder_1 != null) { GunBarrel_1_Pivot = GunHolder_1.transform.GetChild(0).gameObject; }
  28.         if (GunHolder_2 != null) { GunBarrel_2_Pivot = GunHolder_2.transform.GetChild(0).gameObject; }
  29.         if (GunHolder_3 != null) { GunBarrel_3_Pivot = GunHolder_3.transform.GetChild(0).gameObject; }
  30.         if (GunHolder_4 != null) { GunBarrel_4_Pivot = GunHolder_4.transform.GetChild(0).gameObject; }
  31.     }
  32.  
  33.  
  34.     // Start is called before the first frame update
  35.     void Start()
  36.     {
  37.         GetGunHolders();
  38.     }
  39.  
  40.     // Update is called once per frame
  41.     void Update()
  42.     {
  43.         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  44.  
  45.         float h = Input.GetAxis("Horizontal");
  46.         float v = Input.GetAxis("Vertical");
  47.  
  48.         Vector3 tempVect = new Vector3(h, v, 0);
  49.         tempVect = tempVect.normalized * (moveSpeed * Time.deltaTime) * speedMultiplicator;
  50.         rb.MovePosition(rb.transform.position + tempVect);
  51.  
  52.  
  53.         float AngleRad = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x);
  54.         float angle = (180 / Mathf.PI) * AngleRad;
  55.  
  56.         float angle1 = Mathf.Clamp(angle, -62, 126);
  57.         float angle2 = Mathf.Clamp(angle, -126, 62);
  58.         float angle3 = Mathf.Clamp(angle, -162, 62);
  59.         float angle4 = Mathf.Clamp(angle, 18, 118);
  60.  
  61.         GunBarrel_1_Pivot.transform.rotation = Quaternion.AngleAxis(angle1, Vector3.forward);
  62.         GunBarrel_2_Pivot.transform.rotation = Quaternion.AngleAxis(angle2, Vector3.forward);
  63.         GunBarrel_3_Pivot.transform.rotation = Quaternion.AngleAxis(angle3, Vector3.forward);
  64.         GunBarrel_4_Pivot.transform.rotation = Quaternion.AngleAxis(angle4, Vector3.forward);
  65.     }
  66. }
Advertisement
RAW Paste Data Copied
Advertisement