Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Help with spinning cube on trigger

Discussion in 'Scripting' started by Gary_B, Sep 7, 2017.

  1. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    Hi,

    I'm completely new to coding so any help would be of great value.

    I am trying to get a cube (which is an imported FBX with materials) to spin when the collider enters the box collider of the cube. I have been following 2 tutorials to get me going. The first is a spinning cube, which I completed successfully and the second is adding sound/music to the game when a 'Trigger Zone' is entered, again, this was no problem.

    I would now like the cube to spin when the trigger zone is entered, so I thought I would be able to combine the 2 and make it work, but after a good while I've come to a dead end. Due to my lack of understanding I have no idea why what I have written isn't working when the script is added to the cube.

    The cube tutorial I followed - https://unity3d.com/learn/tutorials/topics/scripting/spinning-cube

    The music tutorial I followed - https://unity3d.com/learn/tutorials/topics/audio/adding-music-your-game

    And my unsuccessful attempt at a spinning cube based on combining the above -

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class TriggerSpinControl : MonoBehaviour
    7. {
    8.     public float speed1 = 25f;
    9.     public float speed2 = 100f;
    10.    
    11.  
    12.     void OnTriggerEnter(Collider other)
    13.         {
    14.             if (other.CompareTag("TriggerZone"))
    15.  
    16.             {
    17.  
    18.              transform.Rotate(Vector3.forward, speed1 * Time.deltaTime);
    19.             }
    20.         }
    21.  
    22.         void OnTriggerExit(Collider other)
    23.         {
    24.             if (other.CompareTag("TriggerZone"))
    25.             {
    26.  
    27.             transform.Rotate(Vector3.forward, speed2 * Time.deltaTime);
    28.             }
    29.  
    30.         }
    31.  
    32.    
    33. }
    34.  
    Any help would be appreciated.

    Thanks
     
    Last edited: Sep 7, 2017
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Where are the Debug.Log statements?
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you look at an example of transform.rotate, you will see it's in an update function. That function is essentially a loop. The trigger function only fires once. You would need to either use the update, by way of a boolean value, or start a coroutine.
     
  4. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    There isn't one. Purely because neither of the above tutorials have one in them.


    I see, thanks for your reply. I understand why it's not working now.

    So can I just amend the above script to call for a spin script in place of the transform.rotate, one for a fast spin and one for a slow spin?

    I'll have a research and attempt later today.

    Thanks again
     
  5. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    I have tried creating a script (based on various bits and pieces of of information from other questions and answers) that calls for 2 Spin scripts to be run on the TriggerEnter and TriggerExit, but since I'm still trying figure out and understand what I'm doing, it doesn't seem to be working.

    The spin scrips are not attached to a game object but are just in the Assets folder.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class TriggerSpinControl : MonoBehaviour
    7.  
    8. {
    9.     private SpinFast spinfast;
    10.     private SpinSlow spinslow;
    11.  
    12.  
    13.         void OnTriggerEnter(Collider other)
    14.         {
    15.         if (other.CompareTag("TriggerZone"))
    16.  
    17.             {        
    18.  
    19.                spinfast = GetComponent<SpinFast>();
    20.             }
    21.  
    22.         }
    23.  
    24.         void OnTriggerExit(Collider other)
    25.         {
    26.             if (other.CompareTag("TriggerZone"))
    27.             {
    28.  
    29.               spinslow = GetComponent<SpinSlow>();
    30.          
    31.             }
    32.  
    33.         }
    34.  
    35.    
    36. }
    37.  
    38.  
    It could just be a load of nonsense, but I'm hoping I'm on the right track.

    Any beginner friendly help or guidance of where I'm going wrong so I can understand it a little better would be greatly appreciated

    Thanks
     
    Last edited: Sep 8, 2017
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I suggest you to get some tutorials online on "how to" basics. How to move stuff, how to rotate stuff.
    Here's official tutorials link, there's plenty of usefull stuff:
    https://unity3d.com/learn
     
  7. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    Thanks, yeah, I'm dipping in and out of the tutorials as I need them. I'll be spending a bit more time on them soon, it's just I wanted to do something I haven't yet covered and so become stuck
     
  8. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Those aren't components if they are in the asset folder. You have to add them to the game object. Normally, for something like that a separate script isn't necessary. You just create a bool variable in the same script:
    bool spinfast = false;
    bool spinslow = false;

    OnTriggerEnter(){
    spinfast = true;
    }

    Update(){

    if(spinfast){
    //spinfast code
    }
    else if(spinslow){
    //spinslow code
    }
    }

    Of course you can create separate scripts.
     
    Gary_B likes this.
  9. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    You are going way too complex for a simple script just for spinning, You only need one script attached to the cube with something like the following.

    Code (CSharp):
    1.  
    2.     public float speed1 = 25f;
    3.     public float speed2 = 100f;
    4.  
    5.     void OnTriggerStay(Collider other)
    6.     {
    7.         if (other.tag == "Player")
    8.  
    9.         {
    10.  
    11.             transform.Rotate(Vector3.up, speed1 * Time.deltaTime); // Vector3.up spins on Y
    12.         }
    13.     }
    14.  
    15.     void OnTriggerExit(Collider other)
    16.     {
    17.         if (other.CompareTag("TriggerZone"))
    18.         {
    19.  
    20.             transform.Rotate(Vector3.up, speed2 * Time.deltaTime);
    21.         }
    22.  
    23.     }
    1. Make sure the collider on the cube is set to "IsTrigger" (Only issue is that the player/object will clip through it)
    2. OnTriggerStay Will run as long as the object is in the trigger area.
    3. Make sure object that will trigger is tagged appropriately, such as "Player";

    You might also have to make the box collider on the cube bigger.
     
    Gary_B likes this.
  10. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Rob21894, that's basically what he had and it didn't work, neither would your script, because a continual rotate requires some type of loop. Yours would only make a small step that wouldn't be visible. A trigger only fires once.
     
  11. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    OnTriggerStay updates continuesly as i said, acts as an update loop as long as you're inside the trigger area

    https://gyazo.com/4b5f5c95c843631d574f749817024f79

    Works fine here with the script I posted.
     
  12. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Oh, didn't see that. Sorry.
     
  13. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    Thank you for all your help and suggestions.

    @Rob21894 You're suggestion worked when I entered the trigger but neither before or after,

    @fire7side You got me thinking about true/false statements and how to stop start the Update,

    so a bit of fiddling and messing about I finally came up with this solution that works perfectly.

    What I don't think I had communicated was that I wanted the cube spinning before the trigger is entered - my bad.

    Anyway, my script:-

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TriggerSpinControl : MonoBehaviour
    6.  
    7. {
    8.  
    9.  
    10.     public float speed1 = 25f;
    11.     public float speed2 = 100f;
    12.  
    13.     private void Update()
    14.     {
    15.         transform.Rotate(Vector3.forward, speed2 * Time.deltaTime);
    16.     }
    17.  
    18.     private void OnTriggerEnter(Collider other)
    19.     {
    20.         if (other.tag == "MainCamera")
    21.         {
    22.             enabled = false;
    23.         }
    24.     }
    25.  
    26.     void OnTriggerStay(Collider other)
    27.     {
    28.         if (other.tag == "MainCamera")
    29.  
    30.         {
    31.  
    32.             transform.Rotate(Vector3.forward, speed1 * Time.deltaTime);
    33.         }
    34.     }
    35.  
    36.  
    37.  
    38.     private void OnTriggerExit(Collider other)
    39.     {
    40.         if (other.tag == "MainCamera")
    41.  
    42.         {
    43.  
    44.             enabled = true;
    45.         }
    46.     }
    47.  
    48. }
    49.  
    50.  

    Thanks for you help guys :)

    Edit - I'm using 'MainCamera' as it's use is for the Hololens