Search Unity

BakeMesh creates a Collider for my LineRenderer except its position is far off

Discussion in 'Scripting' started by alekz1k, May 30, 2022.

  1. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    16
    Hello,

    I followed tutorials (attached below) and my grappling hook is working fine in VR.

    Now, I would like to Destroy a GameObject when the grappling hook collides with it.
    I'm using BakeMesh to create a Mesh Collider for the Line Renderer and it seems to work fine, except that the Mesh Collider is created far off/not aligned with the Line Renderer.


    Here is the script I'm using to add the Mesh Collider to the LineRenderer:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. [RequireComponent(typeof(MeshCollider))]
    8. public class LineRendererCollider : MonoBehaviour
    9. {
    10.  
    11.     private LineRenderer lr;
    12.     private MeshCollider lineCollider;
    13.     public GrapplingGun grapplingGun; //I want to create the mesh collider only when the grappling hook is grappling, hence here I grab the bool 'grappled' from another script
    14.    
    15.     private void Start()
    16.     {
    17.         lr = GetComponent<LineRenderer>();
    18.         lineCollider = GetComponent<MeshCollider>();
    19.        
    20.     }
    21.     void Update()
    22.     {
    23.         if (grapplingGun.grappled)
    24.         {
    25.             Mesh lineMesh = new Mesh();
    26.             lr.BakeMesh(lineMesh, Camera.main, true);
    27.             GetComponent<MeshCollider>().enabled = true;
    28.             lineCollider.convex = true;
    29.             lineCollider.sharedMesh = lineMesh;
    30.             Debug.Log("MeshColliderForTheLineRendererHasBeenCreated");
    31.            
    32.         }
    33.         else if (!grapplingGun.grappled)
    34.         GetComponent<MeshCollider>().enabled=false;
    35.        
    36.     }
    37.    
    38. }
    This script is attached to the same GameObject that contains the LineRenderer and the position of that GameObject is 0,0,0 (It's attached to the hoverbike). The Line Renderer uses WorldSpace. I tried to mess around with transform.TransformPoint without success. I'm still learning C# and even tho I feel lost I just don't want to give up on this!! I'd be happy to provide more details and any help would be much appreciated! Have a good one!



    (
    &
    )
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Is the LineRenderer set to use world or local space coordinates?
     
  3. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    16
    The LineRenderer uses World space
     
  4. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    16
    Ok, I'm debugging now and it seems that my GrapplingGun script is the one that gives me the trouble.

    As children of my Hovercraft. I have created a new GameObject with a LineRenderer (World space = false) and, using BakeMesh, I have my Mesh Collider created and aligned with the Line Renderer, which is cool. But when that Line Renderer's Mesh Collider collides with the GameObject I want to Destroy, well nothing happens. I'm pretty sure my Destroy script works and the object I want to destroy has a Box Collider and a Rigidbody attached to it.

    Here is my simple Destroy script:
    Code (CSharp):
    1. public class DestroyCollectable : MonoBehaviour
    2. {
    3.  
    4.     private void OnCollisionEnter(Collision collision)
    5.     {
    6.         if (collision.gameObject.CompareTag("GrapplingHook") || collision.gameObject.CompareTag("Hovercraft"))
    7.         {
    8.             Debug.Log("Destroy that S***");
    9.             Destroy(gameObject);
    10.         }
    11.     }
    12. }
    I know the script works because if my hovercraft (tagged "Hovercraft") collides with the GameObject, the GameObject is destroyed. But when the Line Renderer's Mesh Collider (tagged "GrapplingHook") collides with it, nothing happens. Any idea what I'm missing here?

    EDIT: I had to uncheck 'Kinematic' on my Line Renderer's Rigidbody. Not sure why yet but will look into it.
     
    Last edited: May 31, 2022
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Please don't do that. :) A part of the idea of a forum is that people can search for and find this stuff in the future if they have similar questions. If you delete it that can't happen, which isn't particularly community spirited.

    You'll want to look here.

    best of luck moving forward!
     
    alekz1k likes this.
  6. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    16
    Thank you @angrypenguin, now it all makes sense :)

    "You can move a kinematic rigidbody object from a script by modifying its Transform Component
    but it will not respond to collisions and forces like a non-kinematic rigidbody. "
     
    angrypenguin likes this.