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 Is there a way to use a collider transform rather than an object?

Discussion in 'Scripting' started by TheProcessYet_64, Jul 15, 2023.

  1. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    39
    I am using a character controller and have over 10 different "bounce cube" objects (essentially like bounce pads with different positions and rotations) that all have triggers and when my player interacts with them, my player first rotates towards the specific target bounce cube object and then does specific movements after the rotation is complete.

    So if I have my first bounce cube that by default has (0, 0, 0) transform rotation in the inspector, and a second that is (0, 90f, 0), when I interact with them, only the first cube functions properly because it is the one that I have attached to my inspector via public Transform targetCube. So when I interact with the second cube object, my player still tries to rotate in the direction of the first object.

    But since I am using OnTriggerEnter(Collider col), is there a way to use the col so that I don't need to create 10+ more Transforms? I tried to store the object I collided with and then use its information to do the specific movement that I want, so basically make it the new target but I didn't know where to start.

    Code (CSharp):
    1. public Transform bounceCube;
    2.  
    3. void OnTriggerEnter(Collider col)
    4. {
    5.     if (col.gameObject.tag == "BounceCube")
    6.     {
    7.         willBounce = true;
    8.     }
    9. }
    10.  
    11. void Update()
    12. {
    13.     if (willBounce == true)
    14.     {
    15.         if (Input.GetKey(KeyCode.Space))
    16.         {
    17.             velocity.y = Mathf.Sqrt(bounceHeight * -2f * gravity);
    18.             canLeap = true;
    19.         }
    20.     }
    21.  
    22.     if (canLeap == true)
    23.     {
    24.         StartCoroutine(LeapForward(bounceCube));
    25.     }
    26. }
    27.  
    28. IEnumerator LeapForward(Transform bounceCube)
    29. {
    30.     Quaternion targetRotation = Quaternion.identity;
    31.     do
    32.     {
    33.         Debug.Log("Rotate");
    34.         Vector3 targetDirection = bounceCube.transform.position - transform.position;
    35.         ...
    36.         yield return null;
    37.     }
    38. }
     
    Last edited: Jul 15, 2023
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    col.transform.rotation?
     
  3. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    39
    I tried that also and I used public Collider col; but it produces the same result. I need to drag the collider for one of the objects into the inspector, and the other bounce cube objects still don't work because they are all using that one particular collider. I'm not sure if that was the wrong way to do this
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Uhm you have a public variable called bounceCube. You never change that variable in your code. So I guess you assigned a single object to that variable in the inspector? When you want to support multiple bounce pads you have to grab the transform of the collider that you get inside OnTriggerEnter.

    It probably would make more sense to use OnTriggerStay and do the key down check in there. I'm also not sure the point of all those boolean variables which you never set back to false. Also you would start a new coroutine every frame. That's not how coroutines work. We don't know what your actual movement / rotation logic may look like since that's not included in your code.

    Code (CSharp):
    1.  
    2. void OnTriggerStay(Collider col)
    3. {
    4.     if (col.gameObject.tag == "BounceCube")
    5.     {
    6.         Transform bCube = col.transform;
    7.         if (Input.GetKeyDown(KeyCode.Space))
    8.         {
    9.             velocity.y = Mathf.Sqrt(bounceHeight * -2f * gravity);
    10.             StartCoroutine( LeapForward( bCube ) );
    11.         }
    12.     }
    13. }
    14. IEnumerator LeapForward(Transform bounceCube)
    15. {
    16.     // [ ... ]
    17.     // loop here to do whatever movement necessary
    18.     // make sure you have a yield return null inside the loop.
    19. }