Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [RequireComponent(typeof(Rigidbody2D))]
- public class TankMovement : MonoBehaviour
- {
- private Rigidbody2D rigidbody;
- private float verticalAxis, horizontalAxis;
- // Names input axis
- [Header("Give axis for controling the tank")]
- public string horizontalAxisName, verticalAxisName;
- [Header("Max speed for the tank")]
- public float maxSpeed = 5f;
- // Start is called before the first frame update
- void Start()
- {
- rigidbody = GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update()
- {
- horizontalAxis = Input.GetAxisRaw(horizontalAxisName);
- verticalAxis = Input.GetAxisRaw(verticalAxisName);
- }
- void FixedUpdate()
- {
- Vector2 input = new Vector2(horizontalAxis, verticalAxis);
- Vector2 direction = Vector2.ClampMagnitude(input, 1);
- // Tank move
- rigidbody.AddForce(direction * 20);
- rigidbody.velocity = Vector2.ClampMagnitude(rigidbody.velocity, maxSpeed);
- transform.up = new Vector2(direction.x, direction.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement