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 Program4Life · Aug 19, 2013 at 03:21 AM · movement scriptmove an object

Player vehicle Movement using rigidbody

Hi guys,

Need some real help here for a school project and I am a novice with unity. I am aiming for player movement using rigid body that works similar to a street vehicle. I need physics that accelerates through to a top speed and once in motion I want the object to stay in motion until drag slowly stops it or brakes are applied. The code I have at the bottom moves like a boat in water, it doesn't have good turning and some weird rotation issues. plus no gauge for top speed. So I mainly narrowed it down to three questions for you guys.

1) What should I be using to move rigid body forward and backwards? forces or velocity?

2) What should I be using to turn rigid body while moving forward? forces, torque, or angular velocity?

3) How should I accelerate gradually to a top speed or velocity?

here is my code that I have been doing for about two weeks and tried every kind of combination that I know. So any help with this would really be appreciated.

     var tForce : float = 100;
     var mForce : float = 300;
     var drag : float = 1.5;
     
     var forward : boolean = false; 
     var backward : boolean = false;
     var right : boolean = false;
     var left : boolean = false;
     
     var r_angleVelocity : Vector3 = Vector3 (0,100,0);
     var l_angleVelocity : Vector3 = Vector3 (0,-100,0);
     
     var forwardVector : Vector3;
     var rightVector : Vector3;
     var leftVector : Vector3;
     
     var forwardLeft: Vector3;
     var forwardRight: Vector3;
 
 function Update () 
 
 {
 
     if(Input.GetKey("w"))
     {
         forward = true;
     }
     else
     {
         forward = false;
     }
         
     
     if(Input.GetKey("s"))
     {
         backward = true;
     }
     else
     {
         backward = false;
     }
     
     if(Input.GetKey("d"))
     {
        right = true;
     }
     else
     {
            right = false;
     }
     
     if(Input.GetKey("a"))
     {
        left = true;
     }
     else
     {
            left = false; 
     }
     
 }
 
 function FixedUpdate()
 
 {
 
     var r_deltaRotation : Quaternion = Quaternion.Euler(r_angleVelocity * Time.deltaTime);
      var l_deltaRotation : Quaternion = Quaternion.Euler(l_angleVelocity * Time.deltaTime);
      
      forwardVector = (Vector3.forward * mForce);
      rightVector = (Vector3.right * tForce).normalized;
      leftVector = (Vector3.left * tForce).normalized;
      
      forwardLeft = (forwardVector + leftVector).normalized;
      forwardRight = (forwardVector + rightVector).normalized;
      
      if(forward == true)
      {
          rigidbody.AddRelativeForce(forwardVector,ForceMode.Force);
          
      }
      else
      {
          rigidbody.drag = drag;
      }
      
      if( backward == true)
      {
          rigidbody.AddRelativeForce(Vector3.back * mForce, ForceMode.Force);
      }
      
      
      
      if( right == true)
      {
          
          rigidbody.MoveRotation(rigidbody.rotation * r_deltaRotation);
 
          rigidbody.AddForce(forwardRight, ForceMode.Acceleration);
          
     }
      
      
      if( left == true)
      {
          
          rigidbody.MoveRotation(rigidbody.rotation * l_deltaRotation);
 
          rigidbody.AddForce(forwardLeft, ForceMode.Acceleration);
      }
      
  }
      
Comment
Add comment · Show 3
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
avatar image Benproductions1 · Aug 19, 2013 at 07:56 AM 0
Share

If you want to emulate a car or a vehicle, I suggest you use WheelColliders or your own implementation thereof. You aren't going to get anything that looks good using just standard rigidbody dynamics :)

avatar image Program4Life · Aug 20, 2013 at 02:04 AM 0
Share

well my game is futuristic there are no wheels. I don't care how it looks that's not my portion I just need it to act like a car does. Are you saying that the only way to reach my goal is to add wheel colliders?

avatar image Benproductions1 · Aug 20, 2013 at 02:14 AM 0
Share

If you want realistic vehicle physics that resemble that which we have today, then yes, you should use WheelColliders

If you want realistic physics, then I suggest finding out how your "car without wheels" works and then implement the physics using that.

It really depends on what you want the final outcome to be, and since you have not given such information, all I can do is guess

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by scipiothegreat · Aug 24, 2013 at 02:24 AM

The best way to add force gradually is to use rigidbody.AddForce(). Changing the velocity results in the car going from 0-60 in one frame. For turning use Torque, not angularVelocity. A top speed can made by checking the rigidbody's velocity.magnitude or velocity.sqrMagnitude. Braking and drag can be applied by applying a force in the opposite direction of movement.

Comment
Add comment · Show 1 · Share
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
avatar image Tymrt · Aug 09, 2021 at 07:14 PM 0
Share

The problem kinda isnt how to make the car move, is that, I use the rigidbody.AddFoce() to make it go right or left, but then it either moves perfectly but doesnt rotate at all, which makes the car look like its sliding rather than turning, and by using my solution it turns and even wobbles a bit (drift/drag I assume its called) before stabilizing but instead of stabilizing in the same direction it was going before it turned it now takes another diagonal direction...

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

19 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

Related Questions

move enemy to old position of player 2 Answers

How can i make diagonal movement? 1 Answer

How to make my NPC move automatically on the X-axis. 1 Answer

How can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer

Joystick movement getting stuck. Any Reason why? 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