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.
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.
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.
using UnityEngine;
using System.Collections;
public class TriggerSpinControl : MonoBehaviour
{
private SpinFast spinfast;
private SpinSlow spinslow;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("TriggerZone"))
{
spinfast = GetComponent<SpinFast>();
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("TriggerZone"))
{
spinslow = GetComponent<SpinSlow>();
}
}
}
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
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:
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
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;
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.