Search Unity

raycast on a mesh collider fail

Discussion in 'Scripting' started by EdgarCarrera, May 2, 2016.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Why raycast ignore the mesh colliders?
    i have all LayerMask and no detect

    Code (CSharp):
    1. Vector3 vector2 = 0.5f * (allWheels[0].transform.position + allWheels[1].transform.position) - new Vector3(0f, 0.5f, 1f) * .5f;
    2.         RaycastHit raycastHit;
    3.         isGrounded = Physics.Raycast(vector2, -Vector3.up, out raycastHit, .5f,wheelPhysics);
    View attachment 183727
     
  2. Flunkee

    Flunkee

    Joined:
    Mar 6, 2015
    Posts:
    17
    You have a float set to where an int should be in Physics.Raycast method. It might be throwing it off.
     
  3. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Code (CSharp):
    1. public void LoadVehicleSettings() {
    2.  
    3.         CenterOfMass = new GameObject ("CenterOfMass");
    4.         CenterOfMass.transform.parent = transform;
    5.         //create rigidbody//
    6.         rBody = gameObject.AddComponent<Rigidbody>();
    7.         rBody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
    8.         rBody.mass = 100f;
    9.         rBody.angularDrag = 5f;
    10.         rBody.centerOfMass = CenterOfMass.transform.position;
    11.  
    12.         //set up the wheels//
    13.  
    14.         //rear left wheel//
    15.         Mesh mesh = allWheels[2].GetComponentInChildren<MeshFilter>().mesh;
    16.         colLB = new GameObject("colLB");
    17.         SphereCollider col = colLB.AddComponent<SphereCollider>();
    18.         if(allWheels[2].transform.childCount > 0) {
    19.             colLB.transform.position = allWheels[2].transform.position;
    20.         } else {
    21.             colLB.transform.position = allWheels[2].transform.position + mesh.bounds.center;
    22.         }
    23.         col.material = tirePhysicsMat;
    24.         col.center = new Vector3(0f,-.25f,0f);
    25.         colLB.transform.SetParent(transform);
    26.         Wheel wheelScript = colLB.AddComponent<Wheel>();
    27.         wheelScript.vehicleObj = gameObject;
    28.         wheelScript.wheelObj = allWheels[2];
    29.         wheelScript.wheelLB = true;
    30.         wheelScript.wheelHangDistance = wheelHangDistance;
    31.             allWheels [2].transform.parent = transform;
    32.  
    33.         //front left wheel//
    34.         mesh = allWheels[0].GetComponentInChildren<MeshFilter>().mesh;
    35.         colLF = new GameObject("colLF");
    36.         col = colLF.AddComponent<SphereCollider>();
    37.         if(allWheels[0].transform.childCount > 0) {
    38.             colLF.transform.position = allWheels[0].transform.position;
    39.         } else {
    40.             colLF.transform.position = allWheels[0].transform.position + mesh.bounds.center;
    41.         }
    42.         col.material = tirePhysicsMat;
    43.         col.center = new Vector3(0f,-.25f,0f);
    44.         colLF.transform.SetParent(transform);
    45.         wheelScript = colLF.AddComponent<Wheel>();
    46.         wheelScript.vehicleObj = gameObject;
    47.         wheelScript.wheelObj = allWheels[0];
    48.         wheelScript.wheelLF = true;
    49.         wheelScript.wheelHangDistance = wheelHangDistance;
    50.  
    51.         turnLF = new GameObject("turnLF");
    52.         turnLF.transform.parent = transform;
    53.         turnLF.transform.position = allWheels[0].transform.position;
    54.         turnLF.transform.rotation = allWheels[0].transform.rotation;
    55.         allWheels[0].transform.SetParent(turnLF.transform);
    56.         wheelScript.turnObj = turnLF;
    57.         wheelScript.frontTire = true;
    58.  
    59.         //rear right wheel//
    60.         mesh = allWheels[3].GetComponentInChildren<MeshFilter>().mesh;
    61.         colRB = new GameObject("colRB");
    62.         col = colRB.AddComponent<SphereCollider>();
    63.         if(allWheels[3].transform.childCount > 0) {
    64.             colRB.transform.position = allWheels[3].transform.position;
    65.         } else {
    66.             colRB.transform.position = allWheels[3].transform.position + mesh.bounds.center;
    67.         }
    68.         col.material = tirePhysicsMat;
    69.         col.center = new Vector3(0f,-.25f,0f);
    70.         colRB.transform.SetParent(transform);
    71.         wheelScript = colRB.AddComponent<Wheel>();
    72.         wheelScript.vehicleObj = gameObject;
    73.         wheelScript.wheelObj = allWheels[3];
    74.         wheelScript.wheelRB = true;
    75.         wheelScript.wheelHangDistance = wheelHangDistance;
    76.         allWheels[3].transform.parent = transform;
    77.  
    78.         //front right wheel//
    79.         mesh = allWheels[1].GetComponentInChildren<MeshFilter>().mesh;
    80.         colRF = new GameObject("colRF");
    81.         col = colRF.AddComponent<SphereCollider>();
    82.         if(allWheels[1].transform.childCount > 0) {
    83.             colRF.transform.position = allWheels[1].transform.position;
    84.         } else {
    85.             colRF.transform.position = allWheels[1].transform.position + mesh.bounds.center;
    86.         }
    87.         col.material = tirePhysicsMat;
    88.         col.center = new Vector3(0f,-.25f,0f);
    89.         colRF.transform.SetParent(transform);
    90.         wheelScript = colRF.AddComponent<Wheel>();
    91.         wheelScript.vehicleObj = gameObject;
    92.         wheelScript.wheelObj = allWheels[1];
    93.         wheelScript.wheelRF = true;
    94.         wheelScript.wheelHangDistance = wheelHangDistance;
    95.  
    96.         turnRF = new GameObject("turnRF");
    97.         turnRF.transform.parent = transform;
    98.         turnRF.transform.position = allWheels[1].transform.position;
    99.         turnRF.transform.rotation = allWheels[1].transform.rotation;
    100.         allWheels[1].transform.SetParent(turnRF.transform);
    101.         wheelScript.turnObj = turnRF;
    102.         wheelScript.frontTire = true;
    103.  
    104.         gameObject.tag = "Player";
    105.         BoxCollider col3 = gameObject.AddComponent<BoxCollider>();
    106.         col3.size = new Vector3(3.5f,1.5f,5.5f);
    107.         col3.center = new Vector3(0f,.8f,.5f);
    108.         col3.material = bodyPhysicsMat;
    109.  
    110.         carInfo = GetComponentInChildren<CarInformation> ();
    111.         IsLoadedVehicleSettings = true;
    112.     }
     
  4. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    i'm using spherecollider
     
  5. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Sorry can u watch this threads i need really help on this.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    To be honest, I'm confused about what the problem is. Can you lay it out as simply as possible? Explain the exact problem in detail, highlight any code that's involved using code tags, and include any pictures or videos. Please post these inline so we can see them in context.
     
  7. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Can u check this?
    http://forum.unity3d.com/threads/physics-and-mesh-collider.404808/