Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlchemyApp · Jan 29 at 11:14 AM · controllerwheelcollidertank

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

Wheel Script 1 Answer

How to rotate at the same rpm wheels of a tank? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Wheels Are Too Much Inward 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges