What I am trying to do is have the player “step” on an object (trigger) and have that object call a script that moves an object(s) from Point 1 to Point 2. And if the player hits the collider of one of the moving objects, it restarts the level.
So there are 3 scripts - I think 3 is needed, but if anyone knows how to just make it one, that would be awesome.
This is the script for the object that moves from point 1 to point 2 (this seems to be working)
using UnityEngine;
using System.Collections;
public class Trap : MonoBehaviour
{
public Vector3 point2;
IEnumerator Start ()
{
Vector3 point1 = transform.position;
while (true)
{
yield return StartCoroutine(MoveObject(transform, point1, point2, 3.0));
yield return StartCoroutine(MoveObject(transform, point2, point1, 3.0));
}
}
IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
}
This is the code for the collider that triggers the moving object (this does not work, not sure why)
using UnityEngine;
using System.Collections;
public class TrapTrigger : MonoBehaviour
{
void OnTriggerEnter(Collision other)
{
if (other.tag == "Player")
{
MyScript myScript = other.gameObject.GetComponent<MyScript>();
myScript.enabled = true;
}
}
}
And here is the script for the restart function if the player enters the collider (trigger) of the moving object. (this isn’t working either - might be because of the moving script)
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ColRestart : MonoBehaviour
{
void OnTriggerEnter(Collision other)
{
if (other.tag == "Player")
{
SceneManager.LoadScene(“Level_03”);
}
}
}
any help is very much welcomed,
Thank you