- Home /
How to steer vehicle with mortorTorque or different wheel speed in each wheel (for Tanks movement)
So I'm making a tank controller with wheel collider.
Here's the code I use. (Ignore those //, I just don't want to delete it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class AxleInfo
{
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public bool motor;
public bool steering;
}
public class tankcontrollerv2 : MonoBehaviour
{
public List<AxleInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;
public float motorL;
public float motorR;
//public Transform wheelRollL;
//public Transform wheelRollR;
public float forwardspeed, traversespeed, rotationspeed, maxSpeed;
public void Update()
{
if (Input.GetKey("w"))
{
motorL = maxMotorTorque * Time.deltaTime;
motorR = maxMotorTorque * Time.deltaTime;
}
if (Input.GetKey("s"))
{
motorL = -maxMotorTorque * Time.deltaTime;
motorR = -maxMotorTorque * Time.deltaTime;
}
if (Input.GetKey("a"))
{
motorL = maxMotorTorque * Time.deltaTime;
motorR = -maxMotorTorque * Time.deltaTime;
//transform.Rotate(0, -rotationspeed * Time.deltaTime, 0);
//wheelRollL.Rotate(0, 0, 360 * Time.deltaTime);
//wheelRollR.Rotate(0, 0, -360 * Time.deltaTime);
}
if (Input.GetKey("d"))
{
motorL = -maxMotorTorque * Time.deltaTime;
motorR = maxMotorTorque * Time.deltaTime;
//transform.Rotate(0, rotationspeed * Time.deltaTime, 0);
//wheelRollL.Rotate(0, 0, -360 * Time.deltaTime);
//wheelRollR.Rotate(0, 0, 360 * Time.deltaTime);
}
foreach (AxleInfo axleInfo in axleInfos)
{
if (axleInfo.motor)
{
axleInfo.leftWheel.motorTorque = motorL;
axleInfo.rightWheel.motorTorque = motorR;
}
}
}
}
The script is working fine but it won't steer, Here's a pic
https://drive.google.com/file/d/1yIPD-Fta2CmhJQnGfhv8HNmmf7JdXfBe/view?usp=sharing
Seems that the collider won't let it rotate, Anyone know how to solve this problem? I can't force rotate it with Transform.Rotate either.
Also how to make the wheel less slippery (not rolling down along the slope easily)?
Btw here's my normal tank control script. The only reason I try to use wheel collider is that it has suspension system and better in terrian than box collider (wait? just realized that I haven't try capsule or sphere yet.)
I also have a script for turret and gun barrel rotation and a camera script that try to copy from WarThunder (It don't look the same but close enough for me as a beginner I guess.) I may find somewhere to post them.
Here's sample of my Leopard tank: https://youtu.be/IWysiwlVjZs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class usercontroller : MonoBehaviour
{
public Rigidbody rb;
public Transform transform;
public float forwardspeed, traversespeed, rotationspeed, maxSpeed;
public Transform wheelRollL; //Add a rotating wheel here
public Transform wheelRollR;
//public bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
transform = GetComponent<Transform>();
}
void Update()
{
if (GetComponent<Rigidbody>().velocity.magnitude > maxSpeed)
{
GetComponent<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent<Rigidbody>().velocity, maxSpeed);
}
if (Input.GetKey("w")) //&& (isGrounded))
{
rb.AddForce(transform.right * forwardspeed);
wheelRollL.Rotate(0, 0,-360 * Time.deltaTime);
wheelRollR.Rotate(0, 0,-360 * Time.deltaTime);
}
if (Input.GetKey("s")) // && (isGrounded))
{
rb.AddForce(transform.right * -traversespeed);
wheelRollL.Rotate(0, 0,180 * Time.deltaTime);
wheelRollR.Rotate( 0, 0,180 * Time.deltaTime);
}
if (Input.GetKey("a") )//&& (isGrounded))
{
transform.Rotate(0, -rotationspeed * Time.deltaTime, 0);
wheelRollL.Rotate(0, 0, 360 * Time.deltaTime);
wheelRollR.Rotate(0, 0,-360 * Time.deltaTime);
}
if (Input.GetKey("d"))// && (isGrounded))
{
transform.Rotate(0, rotationspeed * Time.deltaTime, 0);
wheelRollL.Rotate(0, 0, -360 * Time.deltaTime);
wheelRollR.Rotate(0, 0, 360 * Time.deltaTime);
}
}
//void OnCollisionEnter()
//{
// isGrounded = true;
// return;
//}
// void OnCollisionExit()
// {
// isGrounded = false;
// return;
//}
}
The collision detection works weirdly so I put "//" for now so you may make tank fly if you gain enough speed on a certain terian.
Your answer
Follow this Question
Related Questions
cpu version of tank controller script 0 Answers
Best Settings/Config Wheelcollider Tank 0 Answers
Simple Tank Controller 2 Answers
How to move caterpillars of a tank? 1 Answer
Problem with my tank script 1 Answer