0
\$\begingroup\$

i am still new to unity but i am trying to apply a 2d box collider to a 3d model ... i am trying to make a fruit ninja clone with trail renderer and i am using circle collider 2d on the trail so i thought i'd use 2d collider too on the 3d model but it doesn't work i get this error : enter image description here

the fruits are supposed to be cut when the trail hits the collider but it doesn't work if i use a 3d collider and 2d collider throws this error

UPDATE

i started by adding 2 3d models of fruits one is a regular fruit the other is a sliced fruit and then i added rigidbody2d & box collider2d to the regular fruit and rigidbody and box collider to the two slices of the sliced fruit because it only seems reasonable for the sliced one to have 3d collider and rigidbody since it will need some physics to get that " slice explosion force " . then i made prefabs from the two and i made i spawner that will spawn and throw the regular fruits up in front of the camera and made a method to replace the regular fruit with sliced ones every time i press "Space" and it worked fine . this is the method i used :

private IEnumerator SpawnFruits()
{
    while (true)
    {
        yield return new WaitForSeconds(Random.Range(minWait , maxWait));

        Transform spawnLocations = spawnPlaces[Random.Range(0, spawnPlaces.Length)];

        GameObject spawn = Instantiate(fruitsToSpawnPrefabs, spawnLocations.transform.position , spawnLocations.transform.rotation);

        spawn.GetComponent<Rigidbody2D>().AddForce(spawnLocations.transform.up * Random.Range(minForce , maxForce) , ForceMode2D.Impulse);

        Destroy(spawn, 3f);
    }
}

and this is how i create the sliced fruits and replace the regular ones :

public void createSlicedFruit()
{
    GameObject inst = Instantiate(SlicedFruit, transform.position, transform.rotation);

    Rigidbody[] rbOnSliced = inst.transform.GetComponentsInChildren<Rigidbody>();

    foreach ( Rigidbody Rb in rbOnSliced)
    {
        Rb.transform.rotation = Random.rotation;
        Rb.AddExplosionForce(Random.Range(500, 1000), transform.position, fruitsExplosionRadius);
    }

    Destroy(gameObject);
    Destroy(inst, 3f);
}

after that i added the trail renderer and give it rigidbody2d and circle collider 2d and a script to make it follow the mouse :

enter image description here

this is the script :

void Awake()
{
    rb = GetComponent<Rigidbody2D>(); 
}

// Update is called once per frame
void Update()
{
    setBaldeToMouse();
}

private void setBaldeToMouse()
{
    var mousePos = Input.mousePosition;
    mousePos.z = 3;

    rb.position = Camera.main.ScreenToWorldPoint(mousePos);
}

i used this :

mousePos.z = 3;

because my camera is positioned z = -3 .. so i can get the trail to slice in the same z position the fruits are .

and lastly i wrote this script to detect if the trail entered the collider of the fruits so they can be cut but it doesn't work for some reason and i thought maybe this box collider 2d error is what causing that error .

the script for the collision between the trail and the fruits :

private void OnTriggerEnter2D(Collider2D collision)
{
    BladeController blade = GetComponent<BladeController>(); 

    if (!blade)
    {
        return;
    }

    createSlicedFruit();
    Debug.Log("slice !");
}
\$\endgroup\$
6
  • \$\begingroup\$ The error message suggests the problem might be that the collider is too small. Have you tried working at a larger scale to see if that clears the error? \$\endgroup\$
    – DMGregory
    Dec 29 '21 at 15:22
  • \$\begingroup\$ @DMGregory yes i changed the scale many times and i still get the same error \$\endgroup\$ Dec 29 '21 at 15:37
  • \$\begingroup\$ Can you walk us through the steps to create a Minimal Complete Verifiable Example in a new, empty project? Once we can reproduce the error, we can test potential fixes to be sure they'll work for you. \$\endgroup\$
    – DMGregory
    Dec 29 '21 at 15:38
  • \$\begingroup\$ Is all this content about how you explode the fruit absolutely required to reproduce the collider error you're observing? Or could you trim this down to make a MINIMAL complete verifiable example? \$\endgroup\$
    – DMGregory
    Dec 29 '21 at 17:36
  • \$\begingroup\$ @DMGregory i really don't know i didn't notice the error until it didn't work \$\endgroup\$ Dec 29 '21 at 20:46

You must log in to answer this question.

Browse other questions tagged .