• 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 CallToAdventure · Nov 15, 2011 at 06:31 PM · carvehicleslope

Vehicle setup - driving slopes

Hi there,

I got this wee script that does most of the vehicle driving job rather well. However, when I am trying to drive a steep (not too steep really) slope, the engine shuts down (as intended). But when I power it on again, I just can't climb up - my vehicle rolls down on wheels.

 float acc;
     if ((Input.GetAxis("Vertical") > 0.1f) || (Input.GetAxis("Vertical") < -0.1f))
     {
         acc = Input.GetAxis("Vertical");
     }
     else
     {
         var localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
         if (localVelocity.z > 0.05)
         {
             acc = -3f;
         }
         else if (localVelocity.z < -0.05)
         {
             acc = 3f;
         }
         else
         {
             acc = 0;
         }
 
     }

I tried something rather crude, a key binding to increase the acceleration - but its not exactly what I am looking for - once used, it will just shoot the vehicle forward - and a subsequent jump over the edge of the slope. What I am really looking for is a steady speed, slowly rolling the slope. I am using the wasd keys - and when I hit the critical point I want to use the shift like a really basic gearbox. Maybe I should look into motor torque instead (I did so with about the same results) or forward friction - if tat's even a variable accessible in script?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CallToAdventure · Nov 21, 2011 at 02:19 PM

This seems to work well but I can't figure out how to drive backwards now. Stuck. But so far it's doing precisely what I needed. As soon as the angle of attack increases I use a secondary key to change gears (fake gearbox) and as soon the slope ends, I get back to the initial speed and the vehicle won't just jump over the edge.

             rigidbody.drag = rigidbody.velocity.magnitude / 1000;

             float angle = 0.0f;

             Vector3 axis = Vector3.zero;

             rigidbody.rotation.ToAngleAxis(out angle, out axis);

               float _x_angle = Mathf.Abs(angle*axis.x);

             _x_angle = Mathf.Clamp( _x_angle, 0f, 10f);

             acc = Mathf.Lerp(accSlope, accAngle, _x_angle/10f );
Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Car goes a bit left when moving 1 Answer

Standard Asset Car won't shift past 3rd gear yet coding is set to 5 gears. 1 Answer

Steering an AI vehicle to a waypoint using float of -1 to 1? 1 Answer

Getting contact force between two rigidbodies 1 Answer

Raycast align with ground normal 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges