Tank drivin'

Hi guys,

working on a little game where you drive a tank around, and am a bit lost on the coding as im still fairly new to it, ive got this so far -

var speed = 100;


function Update () {
	
		moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		spinDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
		spinDirection = transform.TransformDirection(moveDirection);
		spinDirection *= speed;

	
				rigidbody.AddRelativeForce(moveDirection);
				rigidbody.AddTorque(spinDirection);

}

In which the spinDirection doesnt work I know, as i’ve kind of misunderstood the usage of moveDirection I think. but when i looked it up, it was a part of the CharacterController scripting, but I dont have a character controller on my tank, and that worked, so I thought i could rename the variable… hasnt worked tho… can someone shed some light, sorry to ask such basic stuff!

Thank you!

  • Will Goldstone[/code]

have you checked out the tank script included with the package joe posted in this thread?

http://forum.unity3d.com/viewtopic.php?t=4762

i haven’t had the chance to check it out myself but hopefully that helps ; )

Cool, will check it out now, thanks man!

have had a look, seems a bit complex for what i want to do, I dont want a top that rotates with the mouse, and on trying to extract the part for driving i could not understand, and it seemed to create wheel colliders in the script.

Can anyone tackle the basic scripting that I’ve done?

thoroughly NOT tested and quickly butchered from another script i had. also not sure if it’s exactly what you’re trying to do. meant to rotate an object with the mouse or AD/arrow keys. Move forward/backwards with the WS/arrow keys.

var speed = 6.0;
var smooth = 2.0; 
var maxSteeringAngle = 30.0;

private var moveDirection = Vector3.zero;
private var currentRotation : Quaternion;
private var targetRotation : Quaternion;
 	
function Update()
{
	currentRotation = transform.rotation;
	targetRotation = Quaternion.Euler(0,  Input.GetAxis("Horizontal") * maxSteeringAngle, 0); 
	transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * smooth); 

}

function FixedUpdate() 
{
	moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
	moveDirection = transform.TransformDirection(moveDirection);
	rigidbody.AddForce(moveDirection * speed);

}

function Start()
{
	if (rigidbody)
	{
		rigidbody.freezeRotation = true;
	}
}

And it also might help to explain what you mean by “In which the spinDirection doesnt work”. Does nothing at all happen? Does something happen that you don’t expect? Other?

thank u for that, ive tried it… it doesnt seem to do anything at all rotation wise!.. not sure what im missing…

Hi Tom,

thanks for the reply, hope you’re enjoying the new post, my spinDirection variable does nothing at all, sorry if i wasnt specific enough!

cheers,

Will Goldstone

oops. here:

var speed = 6.0;
var smooth = 2.0;
var maxSteeringAngle = 30.0;

private var moveDirection = Vector3.zero;
private var currentRotation : Quaternion;
private var targetRotation : Quaternion;
    
function Update()
{
	currentRotation = transform.rotation;
	targetRotation = Quaternion.Euler(0,  Input.GetAxis("Horizontal") * maxSteeringAngle, 0);

	currentRotation.eulerAngles.y += targetRotation.eulerAngles.y;

	transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * smooth);
}

function FixedUpdate()
{
	moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
	moveDirection = transform.TransformDirection(moveDirection);
	rigidbody.AddForce(moveDirection * speed);
}

function Start()
{
	if (rigidbody)
	{
		rigidbody.freezeRotation = true;
	}
}

here’s what it should do. mouse movement is not active in this. you could change Horizontal to mousemovement in the input manager but it’s kind of jerky.
http://www.oculardgi.com/steeringSample.html

[edit: ps the way i’m adding force probably isn’t the best solution for a tank. the object will slide sideways due to inertia. you might be able to tweak drag etc but maybe just moveDirecton *= speed or something will work better for you]

ok i had amended the first post to this -

var speed = 30.0;
var smooth = 2.0;
var maxSteeringAngle = 90.0;

private var moveDirection = Vector3.zero;
private var currentRotation : Quaternion;
private var targetRotation : Quaternion;

function Update(){
	
	currentRotation = transform.rotation;
	targetRotation = Quaternion.Euler(0, Input.GetAxis("Horizontal") * maxSteeringAngle, 0);
	
	transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, Time.deltaTime*smooth);

}

function FixedUpdate(){
	moveDirection = new Vector3(0,0, Input.GetAxis("Vertical"));
	moveDirection = transform.TransformDirection(-moveDirection);
	rigidbody.AddForce(moveDirection * speed);
}

function Start(){
	if(rigidbody){
		rigidbody.freezeRotation = true;
	}
}

i’m gona try yours now, but from your test it seems to do the same as what i have here- HOWEVER! i really want the tank to keep rotating, not snap back… what can i do…?

Thanks loads for your help man, much appreciated

Cheers

Will

Pete,

just bunged your script on, turning is much better, but the forward back doesnt work, but not to worry i’ll figure it out!, cheers again

  • Wil

figured it out. just way too low a value, cheers again pete!

glad it helped. in case you missed my edit, you may want to look at an alternate method of moving the tank. the way i’m adding force causes the object to slide around turns due to inertia. tanks don’t usually drift through turns but hmmm that does kind of sound fun!

no worries man, its kind of a sci fi tank… so slidey controls look cool! cheers man

slidey controls + big guns = :smile:

Oh there will be big guns alright, you could say i’ll be… bringing them… out…

Got another query for ya, I’ve got two spotlights (i know this isnt a scripting query but im trying to keep my many posts to a minimum) on the front of my tank but as i drive around they randomly go out, this is not an error as far as i can see… theyre just disappearing AND there is no change in the inspector - they dont become disabled or anything.

Test is here -

www.willgoldstone.com/impact

Cheers!

it’s got something to do with your terrain but it’s weird. looks like just a flat plane - 2 tris? i could find a line where you could turn them on and off. maybe angle them down a degree? don’t know.

Seems like a hover tank. Is that the intention? Looks pretty cool so far. For performance reasons you should create a spotlight cookie and use one spotlight instead of two. I don’t know why they disappear but I assume it is because of your terrain mesh. The light thinks it can cull the mesh when it actually shouldn’t because the triangles are so big that something messes up.

yes its just a massive cube for testing created in unity so its 2 tris, thatll explain it, i’ll angle em down too and make a cookie for it soon, thanks guys!