Hey guys!
I have a couple of questions and i’m pretty new to coding, so bear with me.
In my game, the player moves around and collects “coins” and ultimately tries to reach an end point to advance onto the next level. I also want to add in some sort of teleporter which requires the player to have a certain score value in order to use.
Currently I have a script which contains my movement and score collection code, and I also have a teleporter script which identifies when then player enters and returns “Teleport!” in Console.
Question 1: Is it a better idea to have my movement, coin collection, teleporter, and most/all other player interactions on one script? I feel like that would make my game assets cleaner and would probably be easier to code?
Question 2: How do I go about coding the teleporter to translate the player to a specific location, and vise versa from the other side of the teleporter? Ultimately i’d love to be able to have multiple teleporters in a single level (each linked to a single “receiver”, so not like an interconnecting web) so how should I go about specifying which locations to translate too? (This is what makes me think individual scripts for each transporter would be better)
Question 3: (If I do individual scripts) Once I have the actual translation code done, how do I check the score value of the player to make sure its high enough? I was thinking I would do something along the lines on declaring the score as an independent variable (I think my terminology is off) so that I can access it from any script.
Well let me know what you guys think, any and all help is definitely appreciated!
Note The “Respawn” tag check is where my end point is going to advance to the next level.
Player
public class Player : MonoBehaviour {
public float MoveSpeed = 4;
public float RotateSpeed = 90;
private int Score;
private float gravity = 20.0F;
// Use this for initialization
void Start () {
Score = 0;
Debug.Log("You're current score is: " + Score);
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.forward * Time.deltaTime * (Input.GetAxis("Vertical") * MoveSpeed));
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal") * RotateSpeed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("coin"))
{
other.gameObject.SetActive(false);
Score += 1;
Debug.Log ("You're score is now " + Score);
}
if (other.gameObject.CompareTag("Respawn"))
{
Debug.Log("Poof!");
}
}
}
Teleporter
public class TransporterScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter (Collider other) {
if (other.gameObject.CompareTag("Player")) {
Debug.Log("Teleport!");
}
}
}