I have two scripts and i have two main problems.
-
How to integrate between the two scripts i mean doing some actions in specific order ?
-
How to control the elevator until where to make move up and it’s speed ?
The first script:
In this script i detect when the player is on a platform in this case a cube gameobject.
Once the player is on it, in the second script RaiseWalls i’m raising a wall using another cube just for the testing. Until this point it’s working fine. I’m using two public static variables one in this script one in the other and one.
I also used here before a animation clip i recorded to move the elevator up and down. But then i’m not sure using the animation clip how to control the elevator things i want like height setting and speed.
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
int counter = 0;
public static bool touched = false;
private float distanceTravelled;
private Vector3 lastposition;
public float moveSpeed = 5;
public float moveDistance = 50;
private void Start()
{
lastposition = target.transform.position;
}
private void Update()
{
if (RaiseWalls.raised == true && touched == true)
{
MoveElevator();
touched = false;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "ThirdPersonController") // "Platform"
{
Debug.Log("Touching Platform");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "ThirdPersonController") // "OnTop Detector"
{
counter = 0;
Debug.Log("On Top of Platform");
target = GameObject.Find("Elevator");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
GameObject go = GameObject.Find("CubeToRaise");
go.GetComponent<RaiseWalls>();
Debug.Log("The button clicked, raising the wall");
touched = true;
}
}
void OnTriggerExit(Collider other)
{
GameObject findGo = GameObject.Find("ThirdPersonController");
findGo.transform.parent = null;
}
void MoveElevator()
{
for (int i = 0; i < moveDistance; i++)
{
distanceTravelled += Vector3.Distance(target.transform.position, lastposition);
if (distanceTravelled != moveDistance)
target.transform.position += target.transform.up * Time.deltaTime * moveSpeed;
}
}
void OnGUI()
{
GUI.Box(new Rect(300, 300, 200, 20),
"Times lift moved up and down " + counter);
}
}
In the second script i raise the wall. Raise i mean changing the cube scale axis y.
using UnityEngine;
using System.Collections;
public class RaiseWalls : MonoBehaviour
{
public GameObject gameObjectToRaise;
public float raiseAmount;
public float raiseTotal = 50;
public float speed = 2;
public static bool raised = false;
// Use this for initialization
void Start()
{
}
void Update()
{
if (DetectPlayer.touched == true)
{
if (raiseAmount < raiseTotal) // i.e. we haven't raised it fully
{
// work out how much to raise it
float raiseThisFrame = speed * Time.deltaTime; // to account for frame rate
// now we cap it to make sure it doesn't go over 50
if (raiseAmount + raiseThisFrame > raiseTotal)
{
raiseThisFrame = raiseTotal - raiseAmount;
}
// add raiseThisFrame to raiseAmount
raiseAmount += raiseThisFrame;
gameObjectToRaise.transform.localScale += new Vector3(0, raiseThisFrame, 0);
}
else
{
raised = true;
}
}
}
}
And this is what i want to do:
-
How to integrate between the scripts without too many bool variables and without make them public static ?
-
How to move the elevator in the DetectPlayer script smooth up to specific point height i will set for example 50 and when it’s reaching back to 50 move the elevator back down to ground ?