Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController_Tank : MonoBehaviour
- {
- public Rigidbody2D rb;
- public GameObject PlayerTank;
- private Vector3 mousePos;
- public float moveSpeed = 5f;
- public float speedMultiplicator = 1f;
- public GameObject GunHolder_1;
- public GameObject GunHolder_2;
- public GameObject GunHolder_3;
- public GameObject GunHolder_4;
- private GameObject GunBarrel_1_Pivot;
- private GameObject GunBarrel_2_Pivot;
- private GameObject GunBarrel_3_Pivot;
- private GameObject GunBarrel_4_Pivot;
- void GetGunHolders()
- {
- if (GunHolder_1 != null) { GunBarrel_1_Pivot = GunHolder_1.transform.GetChild(0).gameObject; }
- if (GunHolder_2 != null) { GunBarrel_2_Pivot = GunHolder_2.transform.GetChild(0).gameObject; }
- if (GunHolder_3 != null) { GunBarrel_3_Pivot = GunHolder_3.transform.GetChild(0).gameObject; }
- if (GunHolder_4 != null) { GunBarrel_4_Pivot = GunHolder_4.transform.GetChild(0).gameObject; }
- }
- // Start is called before the first frame update
- void Start()
- {
- GetGunHolders();
- }
- // Update is called once per frame
- void Update()
- {
- mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- Vector3 tempVect = new Vector3(h, v, 0);
- tempVect = tempVect.normalized * (moveSpeed * Time.deltaTime) * speedMultiplicator;
- rb.MovePosition(rb.transform.position + tempVect);
- float AngleRad = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x);
- float angle = (180 / Mathf.PI) * AngleRad;
- float angle1 = Mathf.Clamp(angle, -62, 126);
- float angle2 = Mathf.Clamp(angle, -126, 62);
- float angle3 = Mathf.Clamp(angle, -162, 62);
- float angle4 = Mathf.Clamp(angle, 18, 118);
- GunBarrel_1_Pivot.transform.rotation = Quaternion.AngleAxis(angle1, Vector3.forward);
- GunBarrel_2_Pivot.transform.rotation = Quaternion.AngleAxis(angle2, Vector3.forward);
- GunBarrel_3_Pivot.transform.rotation = Quaternion.AngleAxis(angle3, Vector3.forward);
- GunBarrel_4_Pivot.transform.rotation = Quaternion.AngleAxis(angle4, Vector3.forward);
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement