x


Simple Tank Controller

I'm trying to create a simple tank based game and basically I wan't to have a tank move forward with W, back with S, turn right with D and left with A in a simple world (the floor will always be the same height and the walls are quite simple) my problem is I've tried to implement it before and always had some problems with it not behaving exactly as it should. My question is what is the best practice way to do it: by using a Rigidbody and AddForce, or by using a Character Controller, or by just manually setting the positions and handling collisions myself?

Thanks, in advance :) - James

more ▼

asked May 27 '11 at 11:52 AM

jamesthornton78 gravatar image

jamesthornton78
16 2 2 4

Hi James, as with many issues in Unity there are many ways to approach it. Part of the solution relies on the kind of terrain / environment the tank will be covering - whats your plan? This may alter whether you should use wheel colliders, primitive colliders, raycasting.. etc?

May 27 '11 at 12:00 PM willgoldstone
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Believe me, the Character Controller is easier to use. Just add the prefab Standard Assets/Character Controllers/First Person Controller to your scene. It has built-in controls which allows left-right and forth-back movimentation, but doesn't turn. You can disable or delete the 3 built-in scripts and add this one:

private var controller: CharacterController;
var speed: float = 6.0;
var turnSpeed: float = 90;

function Start(){

    controller = GetComponent(CharacterController);
}

function Update(){

    var movDir: Vector3;

    transform.Rotate(0,Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime,0);
    movDir = transform.forward*Input.GetAxis("Vertical")*speed;
    // moves the character in horizontal direction
    controller.Move(movDir*Time.deltaTime-Vector3.up*0.1); 
}

It's very simple, but does the job: it goes forth and back, turns to right and left, and DOESN'T jump, which would be very strange for a thank!

more ▼

answered May 27 '11 at 12:59 PM

aldonaletto gravatar image

aldonaletto
55.3k 21 64 260

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2693
x1373
x751
x73

asked: May 27 '11 at 11:52 AM

Seen: 2545 times

Last Updated: May 27 '11 at 01:01 PM