• 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
1
Question by Daniel G · Aug 06, 2013 at 11:08 PM · inputsphere raycast3d tirevehicletire

Help In Making a SphereCast for 3D Tire! Working RayCast Script included!

Hello,

I am wondering if using a Sphere Raycast is a good idea for a vehicle tire? I want to develop a rock Crawler game :D iOS.

DOCS: http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html

Is high speeds a problem? What are the concerns and is it hard to do?

HERE is my FULLY working Raycast Tire system, I would LOVE it if someone could help me transpose it to using SphereCast :D.

CODE:

 var wheels:iWheels[];
 private var skid:Skidmarks;
 private var slip:float=1.0;
 private class iWheels {//Wheel set
     var collider : WheelCollider; 
     var wheelPref : Transform;
     var driveWheel : boolean = false;
     var steerWheel : boolean = false;
     var rearWheel : boolean = false;
     var lastSkidmark : int = -1.0;
     var rotationValue : float = 0.0;
 }
 
 
 
 
 
 function Update () {
         for(var i:int=0; i<wheels.length; i++){//for each wheel
               var hit : RaycastHit;//We will define WHAT the raycast hit here.
              var whPref = wheels[i].wheelPref; //For each of the wheels, we will create a prefference based on if this is a (Steerable wheel, drive wheel, front wheel)
               var ColliderCenterPoint : Vector3 = wheels[i].collider.transform.TransformPoint(wheels[i].collider.center ); // Lets find the wheel colliders CENTER point in a VECTOR 3, and Give that the name "ColliderCenterPoint".
           
           if (Physics.Raycast( ColliderCenterPoint, -wheels[i].collider.transform.up, hit, wheels[i].collider.suspensionDistance + wheels[i].collider.radius ) ) {// Then lets Cast a ray DOWN to the ground. Raycast( Center of wheel collider, Direction in which we cast the ray! Notice (-) symbol! That means casting DOWN, what ever it hits becomes the "hit" variable, the raycast Distance is ONLY as long as the suspension distance of the wheel collider PLUS the wheels Radius!) Thats it :D!
               whPref.position = hit.point + (wheels[i].collider.transform.up * wheels[i].collider.radius); // NOW lets Make the wheel transform (the graphic wheels) rest at the hit point, BUT we **dont** half the tire in the ground, so we mulitply the radius of the tire, to get it to perfectly (touch) the ground.
           }else{
                whPref.position = ColliderCenterPoint - (wheels[i].collider.transform.up * wheels[i].collider.suspensionDistance);//If the Raycast does NOT hit the ground put it at the FURTHEST point on the suspension distance (the HANGING tire effect).
           }
                   whPref.rotation = wheels[i].collider.transform.rotation * Quaternion.Euler( wheels[i].rotationValue, wheels[i].collider.steerAngle, 0 );//Lets make that (graphic Wheel) the same rotation value and speed as that of the wheel collider!! (NEED to see the tire spinning!!).
                   wheels[i].rotationValue += wheels[i].collider.rpm * ( 360/60 ) * Time.deltaTime;// More calculation to get the spin just right on the (Graphic Tire).
           
                   var CorrespondingGroundHit : WheelHit; //IF the wheel is HITTING THE GROUND,
                   wheels[i].collider.GetGroundHit(CorrespondingGroundHit); // Oh forgot, this makes sure we are looking at the wheel collider on the WHEEL collider, NOT the graphic tire.
           
           if (Mathf.Abs(CorrespondingGroundHit.sidewaysSlip)>8.0*Time.timeScale&&skid&&wheels[i].collider.isGrounded||Mathf.Abs(CorrespondingGroundHit.forwardSlip)>0.3*Time.timeScale&&skid&&wheels[i].collider.isGrounded) { //Basically lets see if the tire slipped :P.
                wheels[i].lastSkidmark=skid.AddSkidMark (hit.point,hit.normal,1.0,wheels[i].lastSkidmark);// If soo! then lets make that SKIDMARKS prefab do its thing! (seperate script)...
                slip=Mathf.Abs(CorrespondingGroundHit.sidewaysSlip)/70; //Lets determine HOW much the tire slipped! 
           }else{
                wheels[i].lastSkidmark=-1; //Else if it didnt slip then just make the,
                slip=0.0; // Slip equal 0 :D
         }
     }
 }




Thanks

Daniel

Comment
Add comment · Show 9
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 06, 2013 at 11:32 PM 1
Share

It's quite easy if you have any knowledge of physics. If done correctly, you should have accuracy at any speed. The only problem I see, is that it uses a sphere cast, meaning your tires will be spheres and not cilinders, possibly causing other problems :)

avatar image Daniel G · Aug 06, 2013 at 11:38 PM 0
Share

Yes, This is true, However i saw that P2 is the variable for the capsules length maybe you could make this halved so the sphere is now squished?

avatar image Daniel G · Aug 06, 2013 at 11:38 PM 0
Share

Btw looks like its the same as Ray cast BUT it makes a 3D casting :P so this should be easy!

avatar image Benproductions1 · Aug 06, 2013 at 11:42 PM 1
Share

@$$anonymous$$ G By P2 do you mean the second parameter? It's name is radius and all it does it "make" the sphere smaller, not squashed :)

And yes for all intents and purposes it can be considered a "thick" raycast, however I think the backend is more of a sweeptest :)

avatar image Benproductions1 · Aug 07, 2013 at 12:32 AM 1
Share

I'm having a hard time reading the code, would you $$anonymous$$d improving the format and adding some comments ;)

Show more comments

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

14 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

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Animation won't complete unless button is held 1 Answer

expressions in statements must only be executed for their side-effects 1 Answer

Sidekick 4G keyboard input 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