Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    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