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 /
This question was closed Oct 04, 2015 at 08:53 PM by LearningNot for the following reason:

The question is answered, right answer was accepted or the viable solution at least for now xD

This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by LearningNot · Oct 03, 2015 at 11:25 PM · c#physicsraycastsetupsuspension

Balancing Forces + Gravity

I am having problem coding basic suspension i think i have Physics problem don't know how to approach it. I am trying to suspend a box in air using physics i think i am doing something wrong would like a new set of eyes

 using UnityEngine;
 using System.Collections;
 
 public class Suspenzija : MonoBehaviour {
 
     public float SuspensionRayLength;
     public float SuspensionPower;
     public Rigidbody kart;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void FixedUpdate ()
     {
         //Defining position,  bottom midle of Box 
         Vector3 midlebottom = transform.TransformPoint (new Vector3 (0f, -0.5f, 0f));
 
         //SuspensionRay hitColector and executes ray for difined point
         RaycastHit WhatRayHited;
         if (Physics.Raycast (midlebottom, -transform.up, out WhatRayHited, SuspensionRayLength)) 
             {
             //Compresion Calculation
             float RCompressionRatio = WhatRayHited.distance / SuspensionRayLength;
             //Reversing it for using it as power multiplayer cuz multplaying it with 0.* is /
             float CompressionRatio = 1 - RCompressionRatio;
             //Clamp just to be sure xD
             Mathf.Clamp (CompressionRatio ,0f ,1f);
 
             //Adding Force to counter gravity so Box goes Up
             kart.AddForceAtPosition (Vector3.up * CompressionRatio * SuspensionPower, midlebottom, ForceMode.Impulse);
 
             //Waz geting jumping box like trambolin so i diceded to add counter force so if suspension almost 
             //extended i add force down, hoping for the best but not good solution, NEED HELP
 
             //MY idea for stoping box jumping all time Not WOrking ATM
             if  (CompressionRatio < 0.8f)
             {
                 float  AntiCompressionRatio = 1f * CompressionRatio;
                 //Tryed adding first pure counter force but it dident work so i tryed to kinda limit it 
                 //with math.clamp but still not geting stable suspension MY MAIN ISUE
                 Mathf.Clamp(AntiCompressionRatio,0.1f,0.6f);
                 {
                 kart.AddForceAtPosition (-Vector3.up * AntiCompressionRatio * SuspensionPower, midlebottom,ForceMode.Impulse);
                 }
             }
 
 
             //Green Ray for debuging
             Debug.DrawRay (midlebottom, -Vector3.up, Color.green);
             Debug.Log (WhatRayHited.distance + "Distanca of hit Amortizera");
             Debug.Log (CompressionRatio + "Power of Amortizera");
 
             } 
         else 
             {
             //Red Ray for debugign
             Debug.DrawRay (midlebottom, -Vector3.up * SuspensionPower, Color.red);
             }
     }
 }
 

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

  • Sort: 
avatar image
1

Answer by Bunny83 · Oct 04, 2015 at 05:00 AM

Just increase the drag of your rigidbody.

Your logic just creates an oscillator. As the object falls down it increases in speed (due to constant acceleration caused by gravity). The closer you get to the ground the greater your counter force gets. At the point of zero force (when your counter force equals the gravitational pull) the object's velocity is greatest so it continous moving downwards which will further increase your counter force. The net force is now pointing upwards and will slow down the object until it stops at a point. At this point your up ward force is largest which will accelerate it back upward. Since it's a linear dependency it would bounce up and down forever.

When you add drag to the movement a bit of energy is "lost" each frame. Depending on how large the drag is the faster it will come to a halt at the zero force point.

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Physics.Raycasy not really using Layermask? 1 Answer

Having a RaycastHit event create a component? 1 Answer

thousands of raycasts in nested for loop 0 Answers

Trouble with checking Raycast 1 Answer

Physics2D.Raycast Reflect issue 1 Answer


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