How do you set up the physics for a tank?

My goal is to make some tank like vehicle since there is not a suitable one in unity5 store yet. First I tried wheel collider, could not get it to work.

I tried to use raycasting and fixed joint, result was the same. Tried to change bounce threshold, drag, disable gravity and make my own gravity force.

Problem is it won't stops to jitter and vibration is accumulative.

Create one parent object, make 4 or more wheel object, connect them via fixed joint, better to add all fixed joints to parent object. Add wheel.cs to wheel objects, and you are ready to go.

here is the link to my project

another link to same file

PS: what I forgot was the spring itself, I made a super strong spring with no damp.

This is the new wheel.cs code :

using UnityEngine;
using System.Collections;

public class wheel : MonoBehaviour {
Ray ray;
RaycastHit hit;
Rigidbody rb,prb;
float springF=12000f,spring,springK=20f,gravity=1f,distNow,distDef=4f;
Vector3 temp;
void Start () {
rb = transform.GetComponent<Rigidbody> ();

}

void FixedUpdate () {

if (Physics.Raycast (transform.position, -transform.up, out hit,distDef+1)) {
Debug.DrawLine (transform.position, hit.point, Color.red);

if( Vector3.Distance(hit.point, transform.position)<=distDef){
temp=transform.position - hit.point;//what if car turns on its back
distNow=temp.magnitude;
spring=springF*(1-(distNow/distDef));  //this is the proper spring
Debug.Log(spring);

}

} else {
Debug.DrawLine (transform.position,transform.position-(transform.up*3) , Color.blue);
}

}
}


and this one is my controller script if you want to test it :

using UnityEngine;
using System.Collections;

public class MotorController : MonoBehaviour {

public float motorForce,steer,breakForce;
public float forward,side;
Rigidbody rb;
//public Rigidbody wrb;
Transform hud;
public bool cruise;

void Start () {
motorForce = 120f;
breakForce = 15f;
steer = 20f;
cruise = false;
rb = gameObject.GetComponent<Rigidbody>();
//hud = transform.FindChild ("Hud");

}

void Update () {

forward = Input.GetAxis ("Vertical");
side=Input.GetAxis ("Horizontal");
Mover (forward, side);
if(Input.GetKeyDown(KeyCode.O)){cruise=true;}
if(Input.GetKeyDown (KeyCode.P)||forward!=0){cruise=false;}
//if (Input.GetKeyDown (KeyCode.A) && cruise) {cruise=false;}
//HudUpdater ();
}

void Mover(float f, float s)
{

if (cruise) {
f = motorForce;
} else {
f = f *  motorForce+0.00000000001f;//prevent divide by 0 error
}

s = s * steer;
if (rb.velocity.magnitude <= 10f ) {rb.AddForce (transform.forward * f, ForceMode.Acceleration);}
//rb.AddTorque (f* transform.forward, ForceMode.Acceleration);
Vector3 temp =Vector3.up * (s * f / Mathf.Abs (f));

}
void Breaking(){
rb.AddForce (transform.forward * -1*breakForce, ForceMode.Acceleration);

}
/*void HudUpdater(){
string text;
text="speed="+rb.velocity.magnitude.ToString()+"\n angular="+rb.angularVelocity;
//text = transform.eulerAngles.ToString();
hud.GetComponent<TextMesh> ().text = text;

}*/
void AngularFriction(){

}

}


this is the new code without damping ,still working on it ,not very promising :

using UnityEngine;


using System.Collections;

public class wheel : MonoBehaviour { Ray ray; RaycastHit hit; Rigidbody rb,prb; float spring,distDef=2f,distMax=0.1f, bounce=0.1f; float springF=10000f;//4 wheel ,1000 kg mass

// Use this for initialization
void Start () {
rb = transform.GetComponent<Rigidbody> ();
prb=transform.parent.GetComponent<Rigidbody> ();
}

// Update is called once per frame
void FixedUpdate () {

if (Physics.Raycast (transform.position, -transform.up, out hit, distDef+distMax)) {
Debug.DrawLine (transform.position, hit.point, Color.red);
//Debug.Log(Vector3.Angle(hit.normal ,ray.direction));

if(Mathf.Abs(hit.distance)<distDef + distMax){
spring=springF*Mathf.Pow((distDef-hit.distance),2);
}

} else {
Debug.DrawLine (transform.position,transform.position-(transform.up*3) , Color.blue);

}

}


}

• I removed your "other problems" because they are different questions. Please ask them separately to keep the question focused on a single problem. – Philipp Jul 16 '15 at 7:40
• Did you solve your problem yourself? Then please write your own answer to your own question instead of editing the answer into the question. Stackexchange is not a forum. Posting questions and then answering them yourself is encouraged. – Philipp Jul 17 '15 at 9:27
• its not answered ,i just add a more realistic spring function but damping is my problem. also the simple structure i described above is too sensitive . i am trying to get that with spring joint but the resonance of system wont die easy. – Shahab Mos Jul 17 '15 at 13:57
• i changed the code so the spring uses original linear spring formula F=KX, and put it outside both if statement so it works as a normal spring. – Shahab Mos Jul 18 '15 at 8:54
• and put it outside " if else" statement so it works as a normal spring. but cant make any critical damping formula to work, i think fixed joints are the problem,they are not so "fixed",thy resonance more than my springs does without damping,increasing angular drag is worst fix i can think of . the other problem : the moment this car rolls over the entire fixed joint mess up and car flies into infinity. im so full of unity 5 , now trying not to use fixed joint see how it turnes out. – Shahab Mos Jul 18 '15 at 9:02

protected by Community♦Jun 7 '16 at 22:27

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?