Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Veity · Jul 25, 2014 at 06:56 PM · physicsrigidbodycar

Simple Car Script - Physics makes it wierd

I am trying to create a simple car script, it works fine when driving across flat terrain but when i go over a bump of a certain height it causes the car to fly.

I originally manipulated the transform of the object to make it move, but i then tried changing the velocity of the rigidbody in a fixed update. For somereason this caused the car to firstly move in the wrong direction i.e. back instead of forwards and the car wont turn, it just rotates and flips in the continued direction.

Clearly im missing something here when it comes to manipulating rigidbody velocity but i cant figure out what it is.

Rigidbody Factors:

Mass: 1 Drag: 0.01 Angular Drag: 0.01 use gravity: true

 using UnityEngine;
 using System.Collections;
 
 public class HumveeScript : MonoBehaviour {
 
     Transform Car;
 
     //Movement Information
     public float movespeed = 0.0f;
     public float reverseSpeed = 0.0f;
     public float currentSpeed = 0.0f;
 
     //Controls
     public KeyCode Accelerate = KeyCode.W;
     public KeyCode Decelerate = KeyCode.S;
     public KeyCode SteerLeft = KeyCode.A;
     public KeyCode SteerRight = KeyCode.D;
 
     public bool hasADriver = false;
 
 
     CharacterController controller;
 
     // Use this for initialization
     void Start () 
     {
         Car = transform;
 
         if (rigidbody)
         {
             rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationY;
         }
     }
 
     void FixedUpdate()
     {
         if(movespeed > 0)
         {
             rigidbody.velocity = Vector3.forward * movespeed;
             
             //Car.transform.Translate(Vector3.forward * Time.deltaTime * movespeed);
 
         }
         else if(movespeed < 0)
         {
             rigidbody.velocity = Vector3.back * reverseSpeed;
             
             //Car.transform.Translate(Vector3.back * Time.deltaTime * reverseSpeed);
         }
 
     }
 
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetKey(Accelerate) && hasADriver == true)
         {
             if(movespeed < 0 && reverseSpeed == 0)
             {
                 movespeed += 1;
             }
             else if(movespeed >= 0 && movespeed < 20)
             {
                 movespeed += 0.5f;
             }
             else if(movespeed >= 20 && movespeed < 40)
             {
                 movespeed += 1;
             }
 
 
             if(reverseSpeed == 0 && movespeed < 0)
             {
                 movespeed = 0;
             }
         }
         else
         {
             if(movespeed > 0 && movespeed < 50)
             {
                 movespeed -= 1;
             }
         }
 
         if(Input.GetKey(Decelerate) && hasADriver == true)
         {
             if(movespeed > -50)
             {
                 movespeed -= 1;
             }
 
             if(reverseSpeed < 10 && movespeed <= 0)
             {
                 reverseSpeed += 1;
             }
         }
         else
         {
             if(reverseSpeed > 0)
             {
                 reverseSpeed -= 1;
             }
 
             if(movespeed < 0)
             {
                 movespeed = 0;
             }
 
         }
 
         if (Input.GetKey(SteerLeft))
         {
             if (movespeed < 0)
             {
                 Car.transform.Rotate(Vector3.up, 1.0F);
             }
             else if (movespeed > 0)
             {
                 Car.transform.Rotate(Vector3.down, 1.0F);
                 
             }
         }
         if (Input.GetKey(SteerRight))
         {
             if (movespeed < 0)
             {
                 Car.transform.Rotate(Vector3.down, 1.0F);
             }
             else if (movespeed > 0)
             {
                 Car.transform.Rotate(Vector3.up, 1.0F);
                 
             }
         }
     
     }
 
     void OnCollisionEnter ()
     {
         if(movespeed > 0)
         {
             movespeed = movespeed / 2;
         }
         else if(reverseSpeed > 0)
         {
             reverseSpeed = reverseSpeed / 2;
         }
     }
 }
 
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 LolTroll96 · May 22, 2015 at 05:17 AM

Try changing the mass and the drag. Change the drag to about 5 or something and the mass to 2450, then see if that works. change the angular drag to about 2.5.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

car wheels brakes 2d 1 Answer

Are y-axis wheelcolliders possible? 1 Answer

Wheel Collider - Mesh/Box Collider Problem 0 Answers

Turning Rigid body currently in contact with ground. 1 Answer

Raycast car rig going up ramps/slopes. 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