Advertisement
MyPasteIsWaste

TankController

Apr 1st, 2022
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TankController : MonoBehaviour
  6. {
  7.     private Rigidbody _tankRigidbody;
  8.     private GameObject _barrel;
  9.     private GameObject _turret;
  10.     private GameObject _barrelEnd;
  11.  
  12.     [SerializeField] private float _movementForce = 10f;
  13.     [SerializeField] private float _turnSpeed = 5f;
  14.     [SerializeField] private float _projectileForce = 10f;
  15.  
  16.     [SerializeField] private Camera _mainCamera;
  17.  
  18.     private void Awake()
  19.     {
  20.         _tankRigidbody = gameObject.GetComponent<Rigidbody>();
  21.         _turret = gameObject.transform.GetChild(1).gameObject;
  22.         _barrel = _turret.transform.GetChild(0).gameObject;
  23.         _barrelEnd = _barrel.transform.GetChild(0).gameObject;
  24.         _mainCamera = FindObjectOfType<Camera>();
  25.     }
  26.  
  27.     // Update is called once per frame
  28.     void Update()
  29.     {
  30.         if (Input.GetKeyDown(KeyCode.Space))
  31.         {
  32.             Fire();
  33.         }
  34.  
  35.         TurretLookAtMouse();
  36.     }
  37.  
  38.     private void FixedUpdate()
  39.     {
  40.         if(Input.GetKey(KeyCode.W))
  41.         {
  42.             _tankRigidbody.AddForce(_movementForce * transform.forward);
  43.         }
  44.  
  45.         if (Input.GetKey(KeyCode.S))
  46.         {
  47.             _tankRigidbody.AddForce(_movementForce * -transform.forward);
  48.         }
  49.  
  50.         if (Input.GetKey(KeyCode.D))
  51.         {
  52.             transform.Rotate(_turnSpeed * Vector3.up);
  53.         }
  54.  
  55.         if (Input.GetKey(KeyCode.A))
  56.         {
  57.             transform.Rotate(_turnSpeed * Vector3.down);
  58.         }
  59.     }
  60.  
  61.     private void TurretLookAtMouse()
  62.     {
  63.         Ray _cameraRay = _mainCamera.ScreenPointToRay(Input.mousePosition);
  64.         Plane _groundPlane = new Plane(Vector3.up, Vector3.zero);
  65.  
  66.         if (_groundPlane.Raycast(_cameraRay, out float _rayLength))
  67.         {
  68.             Vector3 _pointToLook = _cameraRay.GetPoint(_rayLength);
  69.             Debug.DrawLine(_cameraRay.origin, _pointToLook, Color.blue);
  70.  
  71.             _turret.transform.LookAt(new Vector3(_pointToLook.x, _turret.transform.position.y, _pointToLook.z));
  72.         }
  73.     }
  74.  
  75.     private void Fire()
  76.     {
  77.         GameObject bullet = ObjectPool.SharedInstance.GetPooledObject();
  78.         if (bullet != null)
  79.         {
  80.             bullet.transform.position = _barrelEnd.transform.position;
  81.             bullet.transform.rotation = _barrelEnd.transform.rotation;
  82.  
  83.             bullet.SetActive(true);
  84.             // Change back to bullet.transform.forward, once rotations are sorted out...
  85.             bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.up * _projectileForce, ForceMode.Impulse);
  86.         }
  87.     }
  88.  
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement