Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tank-Based Creation of Tank-Based Objects

Discussion in 'Physics' started by DerpMerf, Jul 24, 2015.

  1. DerpMerf

    DerpMerf

    Joined:
    Jan 1, 2014
    Posts:
    33
    I'm attempting to create a game that has tanks in it. I've created a "rough sketch" of a script that will probably be overhauled later in the development cycle. It's not working for some reason, probably because rigidbody.AddForce is the most finicky thing since we had to punch holes in paper to program computers. I'd just like to know if I'm going in the right direction or if I should overhaul it now and do something else, or what. Here's the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof (Rigidbody))]
    5. public class TankController : MonoBehaviour {
    6.    
    7.     private Rigidbody rb;
    8.    
    9.     private Vector3 speed;
    10.    
    11.     void Start () {
    12.         rb = GetComponent<Rigidbody> ();
    13.     }
    14.    
    15.     void Update() {
    16.         GetInput ();
    17.     }
    18.    
    19.     void FixedUpdate() {
    20.         MoveTank ();
    21.     }
    22.    
    23.     void GetInput() {
    24.         speed.x = Input.GetAxis ("Horizontal");
    25.         speed.y = rb.velocity.y;
    26.         speed.z = Input.GetAxis ("Vertical");
    27.     }
    28.    
    29.     void MoveTank() {
    30.         rb.velocity = speed;
    31.        
    32.         rb.AddForce(new Vector3(speed.x, 0, speed.z));
    33.  
    34.         Debug.Log ("Speed.x = " + speed.x + ", and Speed.z = " + speed.z);
    35.     }
    36. }
    37.  
     
  2. DerpMerf

    DerpMerf

    Joined:
    Jan 1, 2014
    Posts:
    33
    Sorry bout all the bumps