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 Tymrt · Aug 09, 2021 at 07:07 PM · physicscarcar physics

Cant seem to make this basic 3D car turn properly

So basically im a car on the road, got 3 lanes, it spawns in the middle, it has a rigidbody so I make it go foward with:

 rb.AddRelativeForce(1000 * 4, 0, 0);

Then I want to add the following feature: When the user presses the right key, it moves to the right lane, and if the left key is pressed it moves to the left lane, nothing to complicated but I just cant seem to get the hang of it. I can make it work using transform.position obviously but it just 'teleports' there, I want it to have that cartoon arcady feeling, it turns, moves then re-adjusts itself to face forward. I just cant seem to make it work, i've used transform.translate, LookAt, FromToRotation, etc... Just cant seem to find out how to make it work.

The best solution I have right now is the following:

 public class CarMove : MonoBehaviour {
 
     public Rigidbody rb;
     public Transform car;
     private int direcha;
     private float carZ;
     private Quaternion deltaQuat;
     private Vector3 axis;
     private float angle;
 
     private void Update() {
         if (Input.GetKeyDown("left")) {
             carZ = car.position.z - 4;
             direcha = 1;
         }
         if (Input.GetKeyDown("right")) {
             carZ = car.position.z + 4;
             direcha = 2;
         }
     }
 
     void FixedUpdate() {
         if (direcha == 1) {
             rb.AddRelativeTorque(0,1f,0);
             StartCoroutine(Carro());
             deltaQuat = Quaternion.FromToRotation(new Vector3(car.position.x + 4,0,0), Vector3.right);
             deltaQuat.ToAngleAxis(out angle, out axis);//I add 4 on the line above to make it change lanes
         } else if (direcha == 2) {
             rb.AddRelativeTorque(0, -1f,0);
             StartCoroutine(Carro());
             deltaQuat = Quaternion.FromToRotation(new Vector3(car.position.x - 4,0,0), Vector3.right);
             deltaQuat.ToAngleAxis(out angle, out axis);//I subtract 4 on the line above to make it change lanes
         } else {
             deltaQuat = Quaternion.FromToRotation(new Vector3(car.position.x,0,0), Vector3.right);
             deltaQuat.ToAngleAxis(out angle, out axis);
         }
         rb.AddRelativeForce(1000 * 4, 0, 0);
         
 
         float dampenFactor = 0.5f;//This couple lines 'affect' how the angularVelocity acts
         rb.AddTorque(-rb.angularVelocity * dampenFactor, ForceMode.Acceleration);
 
         float adjustFactor = 0.9f;//Supposedly this couple lines make the car stabilize itself
         rb.AddTorque(axis.normalized * angle * adjustFactor, ForceMode.Acceleration);
     }
 
     private IEnumerator Carro() {
         yield return new WaitForSeconds(1f);
         rb.angularVelocity = new Vector3(0, 0, 0);
         rb.AddTorque(axis.normalized * angle * 100, ForceMode.VelocityChange);
         direcha = 0;
     }
 }

The final product is that when I press the right or left arrow keys the car moves correctly with kinda of a nice feeling to it but it then start going forward on the direction it is facing and does not stabilize itself at all... I've been banging my head all day on this and I really dont know how to change it

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

202 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 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

Animation in a car without affecting the physics? help! 1 Answer

Objects on the stage twitch while moving 0 Answers

The car flies away when wheel collider touch walls or other objects 1 Answer

Need for speed like physics using Edy's vehicle physics? 1 Answer

OnCollisionEnter doesn't work: changing wheelcollider's sideway friction stiffness 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