Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1.  
  2. [RequireComponent(typeof(Rigidbody2D))]
  3. public class TankMovement : MonoBehaviour
  4. {
  5.  
  6.     private Rigidbody2D rigidbody;
  7.     private float verticalAxis, horizontalAxis;
  8.  
  9.     // Names input axis
  10.     [Header("Give axis for controling the tank")]
  11.     public string horizontalAxisName, verticalAxisName;
  12.  
  13.     [Header("Max speed for the tank")]
  14.     public float maxSpeed = 5f;
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         rigidbody = GetComponent<Rigidbody2D>();
  20.     }
  21.  
  22.     // Update is called once per frame
  23.     void Update()
  24.     {
  25.         horizontalAxis = Input.GetAxisRaw(horizontalAxisName);
  26.         verticalAxis = Input.GetAxisRaw(verticalAxisName);
  27.     }
  28.  
  29.     void FixedUpdate()
  30.     {
  31.         Vector2 input = new Vector2(horizontalAxis, verticalAxis);
  32.         Vector2 direction = Vector2.ClampMagnitude(input, 1);
  33.  
  34.         // Tank move
  35.         rigidbody.AddForce(direction * 20);
  36.         rigidbody.velocity = Vector2.ClampMagnitude(rigidbody.velocity, maxSpeed);
  37.  
  38.         transform.up = new Vector2(direction.x, direction.y);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement