- Home /
Scripted tank track bones behave strange
Rigged and scripted Tank track mesh and bones behave strange when tank rotating. Actually bones are problem.I'm using existing script i found on internet and slightly edited it. I'm 3D designer but i'm doing my best with little time i can spare to be better at scripting but these are moments where i just stuck. As more as i rotate whole tank,more they "skew". :/ Setup of wheels is basically standard what we will expect,Unity wheels component,meshes following wheels component y position and bones following wheels meshes position. Since game is for mobile i reduced number of wheels which leaves gap in between wheels which gives trouble on short tall obstacles where track mesh will cut into this obstacle so to avoid that i added wheels there also just to support realistic obstacle crossing.I have track and bones following perfectly wheel meshes on y position but everything else is not so great.Basically i have few players playing my old wartank game from one year ago and i appreciate them so i'm truing to give them nice update. Scene setup and code is real mess and i will highly appreciate if someone have will to help me. Tanktrack video from my fb profile < you can see it on video. Here are some images
Here is script:
#pragma strict
import UnityEngine;
import System.Collections;
public var wheelcolliders :WheelCollider[] = new WheelCollider[10];
public var WheelMeshTransform :Transform[] = new Transform[10];
var MaximumTorque : float;
var leftInput : float;
var rightInput : float;
var Brakes : float;
public var pos : Vector3;
public var quar : Quaternion;
public var Lbone2 : Transform;
public var Lbone3 : Transform;
public var Lbone4 : Transform;
public var Rbone2 : Transform;
public var Rbone3 : Transform;
public var Rbone4 : Transform;
public var LhalfBone_a : Transform;
public var LhalfBone_b : Transform;
public var LhalfWheelCol_a : WheelCollider;
public var LhalfWheelCol_b : WheelCollider;
function Start () {
}
function Update () {
MaximumTorque = 100;
leftInput = 0;
rightInput = 0;
Brakes = 0;
if(Input.GetKey(KeyCode.LeftShift) == true){
leftInput = 1f;
}
if(Input.GetKey(KeyCode.LeftControl) == true){
leftInput = -1f;
}
if(Input.GetKey(KeyCode.RightShift) == true){
rightInput = 1f;
}
if(Input.GetKey(KeyCode.RightControl) == true){
rightInput = -1f;
}
if (Input.GetKey(KeyCode.Space) == true){
Brakes = 300;
}
if(Input.GetKey(KeyCode.LeftShift) == true && Input.GetKey(KeyCode.RightControl) == true){
MaximumTorque = 150;
}
if(Input.GetKey(KeyCode.RightShift) == true && Input.GetKey(KeyCode.LeftControl) == true){
MaximumTorque = 150;
}
//Left tread
wheelcolliders[0].motorTorque = MaximumTorque * leftInput;
wheelcolliders[1].motorTorque = MaximumTorque * leftInput;
wheelcolliders[3].motorTorque = MaximumTorque * leftInput;
wheelcolliders[4].motorTorque = MaximumTorque * leftInput;
wheelcolliders[5].motorTorque = MaximumTorque * rightInput;
//Right tread
wheelcolliders[6].motorTorque = MaximumTorque * rightInput;
wheelcolliders[7].motorTorque = MaximumTorque * rightInput;
wheelcolliders[8].motorTorque = MaximumTorque * rightInput;
wheelcolliders[9].motorTorque = MaximumTorque * rightInput;
//Meshes Left
wheelcolliders[1].GetWorldPose(pos,quar);
WheelMeshTransform[1].position.y = pos.y;
WheelMeshTransform[1].rotation = quar;
WheelMeshTransform[1].position.z = pos.z; //fixing wheel position error
WheelMeshTransform[1].position.x = pos.x; //fixing wheel position error
wheelcolliders[2].GetWorldPose(pos,quar);
WheelMeshTransform[2].position.y = pos.y;
WheelMeshTransform[2].rotation = quar;
WheelMeshTransform[2].position.z = pos.z; //fixing wheel position error
WheelMeshTransform[2].position.x = pos.x; //fixing wheel position error
wheelcolliders[3].GetWorldPose(pos,quar);
WheelMeshTransform[3].position.y = pos.y;
WheelMeshTransform[3].rotation = quar;
WheelMeshTransform[3].position.z = pos.z; //fixing wheel position error
WheelMeshTransform[3].position.x = pos.x; //fixing wheel position error
//faking left wheel rotation
WheelMeshTransform[4].rotation = WheelMeshTransform[1].rotation;
WheelMeshTransform[0].rotation = WheelMeshTransform[1].rotation;
//Half colliders_bones left
LhalfWheelCol_a.GetWorldPose(pos,quar);
LhalfBone_a.position.y = pos.y-0.13;
LhalfWheelCol_b.GetWorldPose(pos,quar);
LhalfBone_b.position.y = pos.y-0.13;
//Meshes Right
wheelcolliders[6].GetWorldPose(pos,quar);
WheelMeshTransform[6].position = pos;
WheelMeshTransform[6].rotation = quar;
wheelcolliders[7].GetWorldPose(pos,quar);
WheelMeshTransform[7].position = pos;
WheelMeshTransform[7].rotation = quar;
wheelcolliders[8].GetWorldPose(pos,quar);
WheelMeshTransform[8].position = pos;
WheelMeshTransform[8].rotation = quar;
//faking right wheel rotation
WheelMeshTransform[5].rotation = WheelMeshTransform[8].rotation;
WheelMeshTransform[9].rotation = WheelMeshTransform[8].rotation;
//Bones left
Lbone2.position.y = WheelMeshTransform[1].position.y-0.2;
Lbone2.position.x = WheelMeshTransform[1].position.x;
Lbone3.position.y = WheelMeshTransform[2].position.y-0.2;
Lbone3.position.x = WheelMeshTransform[2].position.x; //empty boneFixer.x
Lbone4.position.y = WheelMeshTransform[3].position.y-0.2;
Lbone4.position.x = WheelMeshTransform[3].position.x;
//Bones right
Rbone2.position.y = WheelMeshTransform[6].position.y-0.2;
Rbone3.position.y = WheelMeshTransform[7].position.y-0.2;
Rbone4.position.y = WheelMeshTransform[8].position.y-0.2;
for(var WC:WheelCollider in wheelcolliders)
{
WC.brakeTorque = Brakes;
}
}
Your answer
Follow this Question
Related Questions
Quaternion.TransformPoint? 1 Answer
Weird delay on Tween 1 Answer
Transform.position can't be changed smoothly. 2 Answers
Transform vector to other coordinate frame using quaternions 1 Answer
how to script a position 1 Answer