Tank Drive

I want to create an object that can be driven like a tank:
Two keys control forwards and backwards for the track on the left.
Another two keys control forwards and backwards for the track on the right.

To go forward, you’d have to press forward for both sides. Vice versa for backwards.
To turn clockwise, you’d press forward on the left and backwards on the right. Opposite for counterclockwise.

I guess my question is: Is it possible to create a rigidbody and control multiple parts of it (separately)?

[I’ve searched and the problem is I don’t know what keywords to use. If anyone could show me a better way to describe this or point me in a direction, that’d be great. I’d consider myself an advanced programmer, but I’m new to the engine itself.]

I think I understand what you are looking for, but does it need to be that complex?

Can you just rotate the tank if either of the button are pressed and instead move if both pressed?

Anyway I got something similar to what you are looking at by creating an empty object called Tank and two cubes as child called left and right. To the parent object (the Tank empty object) I added a rigid body so that both cubes are treated as one object in terms of physics.
To move this composite object I wrote this script:

using UnityEngine;
using System.Collections;

public class Tank : MonoBehaviour {
	
	public Transform left;
	public Transform right;
	public float power = 10f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetButton("Fire1")){
			Vector3 fwd = left.TransformDirection(Vector3.left);
			gameObject.rigidbody.AddForceAtPosition(fwd * power, left.position);
		}
		if(Input.GetButton("Fire2")){
			Vector3 fwd = right.TransformDirection(Vector3.left);
			gameObject.rigidbody.AddForceAtPosition(fwd * power, right.position);
		}
	}
}

Than I attached it to the Tank object and initialized the left and right transforms with the 2 cubes using the editor.
For the rigid body on the main object I checked the two boxes X and Z for Freeze Rotation.

It rotates left or right or move forward if you press both alt and ctrl keys.

Of course it will need more tweaking but I think overall is what you were asking.

I am too new to Unity so probably there is a better way to do it…

What you describe is exactly what I wanted. Thank you so much.

However, for some reasons I’m having problems implementing it. Here’s the code I have:

	public Transform left;
	public Transform right;
	
	public float leftSpeed = 1;
	public float rightSpeed = 1;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		float ws = Input.GetAxis("Vertical") * leftSpeed;
		float ad = Input.GetAxis("Horizontal") * rightSpeed;
		
		Vector3 dirLeft = left.TransformDirection(Vector3.left);
		Vector3 dirRight = right.TransformDirection(Vector3.left);
		
		rigidbody.AddForceAtPosition(ws * dirLeft, left.position);
		rigidbody.AddForceAtPosition(ad * dirRight, right.position);

It rotates fine, but for some reasons, it stays still when I press the two buttons to make it go forward.

Your script works just fine for me. If I press A+S the “tank” goes forward, if W+D it goes backward.

Probably is to do with your model, I just used the two cubes I suggested above and as they are 1 in scale I changed left and right speed to 10f.

Hmmm that’s what I was thinking it could be the model’s fault. Can you take a look and see what’s wrong?
The cylinders are all oriented the same way. Their rotation is all X=90, Y=0, Z=0. They are all the same in size. The “cube” has rotation X=0, Y=0, Z=0, and it is centered on the robot.
I’ve made sure the leftside wheels are both actually the left wheels and vice versa for the right side.

Increase the __speed values

try 10 like selvin used

Huh? As you can see in the editor picture, leftSpeed and rightSpeed are both at 10. I’ve increased them to hundreds, but all that does is make them spin faster. Moving forwards/backwards still does not work. In fact when I try to make them go from a spinning state to moving straight, it actually slows it down to a stop.

Your leftside and rightside look empty objects, what about their pivot points? The AddForceAtPosition is applied to those…

It will be because of the freeze rotation or something to do with the intersecting colliders i think

edit: anyway try removing the freeze rotation first then see what your script is ACTUALLY doing to the rigid body before locking it again. I find that very helpful trouble shooting these issues.

The rotated cylinders have colliders which probably stop your robot to move in forward direction.

Try to disable colliders on the cylinders and expand the Cube collider to cover the whole robot (including the cylinders).

If you want proper wheels probably this is what you need

but I never used it before.

Id go this route

Wow that is an useful tip. I should have thought of that. :stuck_out_tongue:
Anyway, now I know what the issue appears to be. When I click W, the model flips backwards (the front wheels lift into the air and land behind the back wheels). When I click A, it flips forwards.
Now how would I go about fixing this problem?

I don’t think that’s it. I disabled the cylinder colliders and expanded the cube collider. Same results.
Thanks a bunch for the link on the wheels though. Now my model behaves exactly the way I want it to (when it works aka when not going straight forwards/backwards).

Where is the fun in that? :wink:
The reason I’m going this route is because I plan on introducing differences between the left and right wheel. For example, if the right motors+wheels are not identical to the left, you’d be experiencing different speeds, and the model should drive in a curve as opposed to a straight line. Also, further along the project I’ll try to experiment with putting in mecanum wheels, which allow fixed axis lateral movement.