Hi there.
What I’m trying to do is to have triggers in the scene that will allow the player to rotate the camera around him, only when he is inside one of those objects colliders.
I have multiple object that all have the same script attached (in this instance named RotateArea.cs).
And I want to access all those objects from another objects script component (CameraRotator.cs).
Is there a way in Unity to access all of those at the same time? I’m currently using FindGameObjectWithTag, as it gets an error as soon as I use FindGameObjectsWithTag. Appearently I can’t use .GetComponent after a FindGameObjectsWithTag.
What to do?
CameraRotator
using UnityEngine;
using System.Collections;
public class CameraRotator : MonoBehaviour
{
public float rotateTime = 1.0f;
private bool _isTweening = false;
private CrunchPlatformColliders _cruncher;
private DisablePlayer _disablePlayer;
RotateArea rotateFunction;
void Start ()
{
_cruncher = GetComponentInChildren<CrunchPlatformColliders>();
GameObject player = GameObject.FindGameObjectWithTag("Player");
_disablePlayer = player.GetComponentInChildren<DisablePlayer>();
rotateFunction = GameObject.FindGameObjectWithTag("rotateZone").GetComponent<RotateArea>();
}
void Update(){
if(rotateFunction.isInRotateZone)
{
if (Input.GetKeyDown(KeyCode.Z))
{
rotateTween(90);
}
if (Input.GetKeyDown(KeyCode.X))
{
rotateTween(-90);
}
}
}
private void rotateTween(float amount)
{
if (_isTweening == false)
{
_isTweening = true;
// _cruncher.setPlayerPos();
_disablePlayer.disable();
Vector3 rot = new Vector3(0,amount, 0);
iTween.RotateAdd(gameObject, iTween.Hash(iT.RotateAdd.time, rotateTime, iT.RotateAdd.amount, rot, iT.RotateAdd.easetype, iTween.EaseType.easeInOutSine, iT.RotateAdd.oncomplete, "onColorTweenComplete"));
}
}
private void onColorTweenComplete()
{
_isTweening = false;
_disablePlayer.enable();
_cruncher = GetComponentInChildren<CrunchPlatformColliders>();
//_cruncher.crunchCollidersToPlayer();
}
}
RotateArea
using UnityEngine;
using System.Collections;
public class RotateArea : MonoBehaviour {
public bool isInRotateZone = false;
public GUIText referencetotext;
// Use this for initialization
void OnTriggerEnter ()
{
isInRotateZone = true;
referencetotext.text ="Press Z or X to change lane";
}
void OnTriggerExit()
{
isInRotateZone = false;
referencetotext.text ="";
}
// Update is called once per frame
void Update () {
}
}
Any help is appreciated