- Home /
Tank-Based Creation of Tank-Based Objects
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:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Rigidbody))]
public class TankController : MonoBehaviour {
private Rigidbody rb;
private Vector3 speed;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void Update() {
GetInput ();
}
void FixedUpdate() {
MoveTank ();
}
void GetInput() {
speed.x = Input.GetAxis ("Horizontal");
speed.y = rb.velocity.y;
speed.z = Input.GetAxis ("Vertical");
}
void MoveTank() {
rb.velocity = speed;
rb.AddForce(new Vector3(speed.x, 0, speed.z));
Debug.Log ("Speed.x = " + speed.x + ", and Speed.z = " + speed.z);
}
}
The tank simply isn't moving, which does not make sense considering I print out speed.x and speed.z and they are not 0.
Did you attach it to the Tank gameObject? 'Cause the script works fine here
Attach it to a Cube. It should move. (At least it did here without any modification of the above script)
As I said before the script works. So the problem is in your end. $$anonymous$$aybe your Input settings are not setup correctly. Create a new project. Add a Cube to the Scene -> Attach Script -> Disable Use Gravity in Rigidbody component(Since there is no terrain) -> Press Play. $$anonymous$$ove Cube with WASD or Arrows
You can't add Force or any Physics if Is $$anonymous$$inematic is checked. http://docs.unity3d.com/ScriptReference/Rigidbody-is$$anonymous$$inematic.html You can uncheck is$$anonymous$$inematic or use $$anonymous$$ovePosition ins$$anonymous$$d AddForce. http://docs.unity3d.com/ScriptReference/Rigidbody.$$anonymous$$ovePosition.html