Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Can Raycast collide with Boxcast

Discussion in 'Physics' started by theashbot, Jun 30, 2023.

  1. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    20
    I am trying to simulate physics though a custom script. I am trying to simulate collider using Physics.___Cast. But the colliders are not colliding with each other. After some test I think it is because the Boxcast is not physical. nothing will collide with it only it will collide with other things. is there any way to make a collider though code?
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Why are you simulating physics instead of using Unity's physics? Boxcast is not a collider, hence it won't collide. Yes, colliders can be created, scaled and rotated with code, but why? Better to create colliders in editor and store them in your prefab, then just disable/enable in code.
     
    theashbot likes this.
  3. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    20
    You asked "Why are you simulating physics instead of using Unity's physics?" My answer. The unity physics engine does not really simulate the kind of physics that I am trying to implement.
    You said "Yes, colliders can be created, scaled and rotated with code, but why? Better to create colliders in editor and store them in your prefab, then just disable/enable in code"
    Are you talking about Adding the collider though code or is there any other way?

    Thank you for your replay.
     
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Yes they can be added through MonoBehaviour API with eg
    AddComponent<BoxCollider>()


    But it's often better to rig your prefabs with colliders in the editor. This is because the collider can be accurately scaled and positioned using the Edit Collider button and gizmo. To create a collider like this, add an empty child gameobject to your gameobject, and add the collider to it, then use the gizmo. Once your collider is where and how you like it, disable the component. Then in your script when you need to use the collider, enable it.
     
    theashbot likes this.
  5. theashbot

    theashbot

    Joined:
    Apr 17, 2022
    Posts:
    20
    Ya that is what I thought you were thinking of. It is what I am doing curranty, but why create a child GameObject?

    I found that I can detect collision by using the Physics class, but I also need a collider on every GameObject that I need collision for.

    Thank you for the fast replies.
     
    Last edited: Jul 3, 2023
  6. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    To decouple the collider from the renderer.

    (Sorry, linked a wrong thing here, deleted it..)
     
    Last edited: Jul 3, 2023