leomovskii

Untitled

Oct 5th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Rigidbody))]
  4. public class Tank : MonoBehaviour {
  5.  
  6.     [SerializeField] private float _moveSpeed;
  7.     [SerializeField] private float _turnSpeed;
  8.  
  9.     private Rigidbody _rigidbody;
  10.     private Vector3 _turnInput;
  11.     private float _moveInput;
  12.  
  13.     private void Awake() {
  14.         _rigidbody = GetComponent<Rigidbody>();
  15.     }
  16.  
  17.     private void Update() {
  18.         _moveInput = Input.GetAxis("Vertical") * _moveSpeed * Time.fixedDeltaTime;
  19.         _turnInput.y = Input.GetAxis("Horizontal") * _turnSpeed * Time.fixedDeltaTime;
  20.         if (_moveInput < 0)
  21.             _turnInput.y *= -1;
  22.     }
  23.  
  24.     private void FixedUpdate() {
  25.         var movement = _moveInput * transform.forward;
  26.         _rigidbody.MovePosition(_rigidbody.position + movement);
  27.  
  28.         if (_turnInput.y != 0) {
  29.             var turnRotation = Quaternion.Euler(_turnInput);
  30.             _rigidbody.MoveRotation(_rigidbody.rotation * turnRotation);
  31.         }
  32.     }
  33. }
  34.  
Add Comment
Please, Sign In to add comment