Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RedSuinit · Dec 15, 2019 at 12:40 AM · raycastdampingsuspension

Can't get a raycast suspension system to work properly.

Hey everyone. I am currently trying to get a raycast based suspension system off the ground and I just cant seem to figure out why my physics engine is going bananas. Here is a .gif of what is going on.

https://www.youtube.com/watch?v=kchI-AURdG8

Here is the code snippet:

 void Start()
 {
     car = this.gameObject.GetComponent<Rigidbody>();
     car.centerOfMass = new Vector3(0, -dropCOM, 0);
     RaycastHit fLRayHit;
     RaycastHit fRRayHit;
     RaycastHit rLRayHit;
     RaycastHit rRRayHit;
     if (Physics.Raycast(fLTire.position, -fLTire.up, out fLRayHit, susLength))
         fLPos1 = fLRayHit.distance;
     if (Physics.Raycast(fRTire.position, -fRTire.up, out fRRayHit, susLength))
         fRPos1 = fRRayHit.distance;
     if (Physics.Raycast(rLTire.position, -rLTire.up, out rLRayHit, susLength))
         rLPos1 = rLRayHit.distance;
     if (Physics.Raycast(rRTire.position, -rRTire.up, out rRRayHit, susLength))
         rRPos1 = rRRayHit.distance;
 }

 // Update is called once per frame
 void Update()
 {

 }

 void FixedUpdate()
 {
     RaycastHit ground;
     Ray groundCheck = new Ray(transform.position, -transform.up);

     if (Physics.Raycast(groundCheck, out ground, 10f, groundLayer))
     {
         gravityDir = ground.normal;
         Debug.Log(gravityDir);
         groundRot = ground.transform.rotation.x;
     }

     car.AddForce(gravityDir * gravityVal, ForceMode.Acceleration);

     RaycastHit fLRayHit;
     RaycastHit fRRayHit;
     RaycastHit rLRayHit;
     RaycastHit rRRayHit;

     if (Physics.Raycast(fLTire.position, -fLTire.up, out fLRayHit, susLength))
     {
         float fLPos2 = fLRayHit.distance;
         float velocity = (fLPos1 - fLPos2) / Time.deltaTime;
         float fLRatio = (susLength - fLRayHit.distance) / susLength;
         float susLift = fLRatio * susForce + velocity * susDamp;
         float lift = susLift;
         car.AddForceAtPosition(lift * fLTire.up, fLTire.position, ForceMode.Acceleration);
         fLPos1 = fLPos2;
     }

     if (Physics.Raycast(fRTire.position, -fRTire.up, out fRRayHit, susLength))
     {
         float fRPos2 = fRRayHit.distance;
         float velocity = (fRPos1 - fRPos2) / Time.deltaTime;
         float fRRatio = (susLength - fRRayHit.distance) / susLength;
         float susLift = fRRatio * susForce + velocity * susDamp;
         float lift = susLift;
         car.AddForceAtPosition(lift * fRTire.up, fRTire.position, ForceMode.Acceleration);
         fRPos1 = fRPos2;
     }

     if (Physics.Raycast(rLTire.position, -rLTire.up, out rLRayHit, susLength))
     {
         float rLPos2 = rLRayHit.distance;
         float velocity = (rLPos1 - rLPos2) / Time.deltaTime;
         float rLRatio = (susLength - rLRayHit.distance) / susLength;
         float susLift = rLRatio * susForce + velocity * susDamp;
         float lift = susLift;
         car.AddForceAtPosition(lift * rLTire.up, rLTire.position, ForceMode.Acceleration);
         rLPos1 = rLPos2;
     }

     if (Physics.Raycast(rRTire.position, -rRTire.up, out rRRayHit, susLength))
     {
         float rRPos2 = rRRayHit.distance;
         float velocity = (rRPos1 - rRPos2) / Time.deltaTime;
         float rRRatio = (susLength - rRRayHit.distance) / susLength;
         float susLift = rRRatio * susForce + velocity * susDamp;
         float lift = susLift;
         car.AddForceAtPosition(lift * rRTire.up, rRTire.position, ForceMode.Acceleration);
         rRPos1 = rRPos2;
     }

     inputDevice = InputManager.ActiveDevice;

     float deadzone = deadZone;
     stickInput = new Vector2(inputDevice.LeftStickX, inputDevice.LeftStickY);
     if (stickInput.magnitude < deadzone)
         stickInput = Vector2.zero;
     else
         stickInput = stickInput.normalized * ((stickInput.magnitude - deadzone) / (1 - deadzone));
 }

I want this to just act like a normal suspension system. Any help would be greatly appreciated. I am still fairly new to this stuff.

The reason for the custom gravity situation is that I want the vehicle to be able to drive on the walls and ceiling.

Thanks

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

165 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

Related Questions

How should I make a custom suspension system? 1 Answer

raycast animated suspension 0 Answers

Balancing Forces + Gravity 1 Answer

Mouse aim in orthographic view 0 Answers

raycast rigidbody addforce 2 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