- Home /
How to make a Script for moving rooms
Hi I'm Looking to make a script or series of scripts for moving rooms
basically theres the main room which is fixed and cannot move
how ever the rooms around it can follow certain trajectories based on its current position compared to its destination
i want to create a feel that wen the player does a something
he/she will feel the rooms and even see the rooms moving around the main building to the destination in question
ive tried for a while to figure this out
but no luck
i need help with a script that detects which destination or dock that the room in question currently occupies then i need a script that dictates the room in question's decided destination then i need a script that decides wat trajectories to take in order to get to said destination
i also need a script that says if the player is inside one of the moving rooms then the player moves with the room (as if on a moving platform)
now im not asking for someone to type up a script for me but if they could gimme some advice as to wat needs to be done how i should type it up and maybe event a link to somewhere i can find the answer for myself i just need a bit of help
and ill appreciate any help i can get
here are a few scripts ive tried yet cant get them to work as i want them to (if u can recommend a better script, id be so happy to lose these ones) [code=CSharp]using UnityEngine; using System.Collections;
public class MovingRoom : MonoBehaviour {
public Transform mytransform;
public Transform[] Target;
public int targetnum;
public int Current;
public Transform[] Trajects;
public MovingRoomCTRL roomctl;
public int path;
public float movespeed = 15;
public float[] minepos;
public float[] destpos;
public int whichdestin;
// Use this for initialization
void Start () {
mytransform = this.transform;
roomctl = GameObject.Find ("Main").GetComponent <MovingRoomCTRL> ();
Trajects = new Transform[5];
Trajects [0] = GameObject.Find ("Traject1").transform;
Trajects [1] = GameObject.Find ("traject2").transform;
Trajects [2] = GameObject.Find ("traject3").transform;
Trajects [3] = GameObject.Find ("Traject4").transform;
Trajects [4] = GameObject.Find ("Traject5").transform;
minepos = new float[3];
minepos [0] = mytransform.position.x;
minepos [1] = mytransform.position.y;
minepos [2] = mytransform.position.z;
}
void Update () {
if (roomctl.route){
WhichDest ();
setthepath ();
Move ();
}
}
public void WhichDest (){
if (minepos [0] != roomctl.Dests [whichdestin].position.x && minepos [1] != roomctl.Dests [whichdestin].position.y && minepos [2] != roomctl.Dests [whichdestin].position.z) {
whichdestin += 1;
} else {Current = whichdestin;}
if (whichdestin > 4)
whichdestin = 0;
}
public void setthepath () {
if (roomctl.selectedDest = roomctl.Dests [0]){
path = roomctl.Destnum;}
else if (roomctl.selectedDest = roomctl.Dests [1]){
path = roomctl.Destnum;}
if (path == 0 && Current == 2) {
Target = new Transform[4];
Target[0] = Trajects[3];
Target[1] = Trajects[2];
Target[2] = Trajects[1];
Target[3] = roomctl.selectedDest;
}
else if (path == 1 && Current == 2) {
Target = new Transform[4];
Target[0] = Trajects[3];
Target[1] = Trajects[2];
Target[2] = Trajects[1];
Target[3] = roomctl.selectedDest;
}
}
public void Move () {
mytransform.position = Vector3.MoveTowards (mytransform.position, Target[targetnum].position, movespeed * Time.deltaTime);
Trajectories ();
}
public void Trajectories (){
if (mytransform.position.y == Target [targetnum].position.y && mytransform.position.x == Target [targetnum].position.x && mytransform.position.z == Target [targetnum].position.z)
targetnum += 1;
if (mytransform.position.y == roomctl.selectedDest.position.y && mytransform.position.x == roomctl.selectedDest.position.x && mytransform.position.z == roomctl.selectedDest.position.z){
roomctl.route = false;
targetnum = 0;
}
}
}[/code]
[code=CSharp]using UnityEngine; using System.Collections;
public class MovingRoomCTRL : MonoBehaviour {
public MovingRoom moverom;
public bool route = false;
public Transform[] Dests;
public int Destnum;
public Transform selectedDest;
void Start () {
moverom = GameObject.FindGameObjectWithTag ("Room").GetComponent <MovingRoom> ();
Dests = new Transform[4];
Dests [0] = GameObject.Find ("Dest1").transform;
Dests [1] = GameObject.Find ("Dest2").transform;
Dests [2] = GameObject.Find ("Dest3").transform;
Dests [3] = GameObject.Find ("Dest4").transform;
}
void Update () {
if (! route) {
if (Input.GetKeyDown (KeyCode.A)) {
Destnum = 0;
route = true;
}
else if (Input.GetKeyDown (KeyCode.B)) {
Destnum = 2;
route = true;
}
} else {SelectDest ();}
}
public void SelectDest (){
selectedDest = Dests [Destnum];
}
} [/code]
P.S I must ask for only C# scripting i cant use javascript so please if u direct me to a tutorial or show me a script please please make sure its C# thank you again
Your answer
Follow this Question
Related Questions
automatic moving script 1 Answer
Moving the hand of my mesh and associated rotations 0 Answers
objects to move when picked up 0 Answers
iOS Tap to move?? 2 Answers