- Home /
How to Lerp several objects positions with one trigger event?
I'm an absolute beginner in Unity and scripting/coding in general, but i'm really determined to learn this stuff. So be gentle if my wording and terminology doesn't quite make sense! :) (And BTW english is not my first language either.)
For my first project (well, self-learning assignment) to get my head around unity and game building in general, i'm making a simple maze game. But to make it more difficult (for the player to make it through and for me to code), i'm going to make spots in the maze, once stepped upon, which will make the maze's walls shift to different pattern.
Here's the test code for a trigger object which moves one wall:
using UnityEngine;
using System.Collections;
public class WallMove : MonoBehaviour {
public float smooth = 50f;
private Transform Wall;
void Start () {
Wall = GameObject.FindGameObjectWithTag("Walls").transform;
}
// Use this for initialization
void OnTriggerStay (Collider other) {
Vector3 initPos = Wall.transform.position;
Vector3 newPos = new Vector3(1, initPos.y, initPos.z);
Wall.transform.position = Vector3.Lerp(initPos, newPos, smooth * Time.deltaTime);
}
}
So now: how to do it for ~20 walls?
1st idea:
I'm thinking i should make a (sort of) database script where i would store the positions (and maybe scale) of individual walls for different maze pattern(s), which will be referenced in the trigger's script.
Problems:
1) How do i reference in script a particular object? FinObjectWithTags would return all the walls and apply the same position to all of them?
2) If i would store all the positions of a pattern in an array (for example: pattern1 = array([wall1.pos], [wall2.pos]), would Lerp(pattern1, pattern2, length*time) actually work?
2nd idea: If it wouldn't work, what else? Just make a different script for each wall? That would mean, that i'd have to make a script for the trigger object, which, when triggered, would return a boolean, which could be checked by each wall?
Problems:
1) Quite obvious: The more scripts, the messier, right?
What help i need? Hard to describe exactly... Some other ideas. Just the general idea for how to plan, how to build this (?)system. What tutorials to search for. What to read upon in Unity's documentation. Not knowing how to begin with this, i really can't google for it, cause i do not know what to look for.
Your answer
