Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class Tank : MonoBehaviour {
- [SerializeField] private float _moveSpeed;
- [SerializeField] private float _turnSpeed;
- private Rigidbody _rigidbody;
- private Vector3 _turnInput;
- private float _moveInput;
- private void Awake() {
- _rigidbody = GetComponent<Rigidbody>();
- }
- private void Update() {
- _moveInput = Input.GetAxis("Vertical") * _moveSpeed * Time.fixedDeltaTime;
- _turnInput.y = Input.GetAxis("Horizontal") * _turnSpeed * Time.fixedDeltaTime;
- if (_moveInput < 0)
- _turnInput.y *= -1;
- }
- private void FixedUpdate() {
- var movement = _moveInput * transform.forward;
- _rigidbody.MovePosition(_rigidbody.position + movement);
- if (_turnInput.y != 0) {
- var turnRotation = Quaternion.Euler(_turnInput);
- _rigidbody.MoveRotation(_rigidbody.rotation * turnRotation);
- }
- }
- }
Add Comment
Please, Sign In to add comment