- Home /
The question is answered, right answer was accepted
[Answered] Level Complete?
I'm working on a 2D platformer, but I can't figure out how to make the player complete a level. I'm thinking that it'll involve using the flag's collider as a trigger, but I just can't make it work. Here's my code so far; if you have any ideas on how I could make something like that work, feel free to share.
using UnityEngine;
using System.Collections;
public class PlayerScript1 : MonoBehaviour {
public Rigidbody2D rb;
public Animator animator;
public float moveSpeed;
public float jumpPower;
public Transform respawnPoint;
public GameObject finish;
private bool faceLeft = false;
void Start () {
gameObject.GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator> ();
}
void MoveAnimate () {
Vector2 vect = GetComponent<Rigidbody2D> ().velocity;
if (Input.GetKey ("d")) {
vect.x = moveSpeed;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", true);
animator.SetBool ("Jumping", false);
animator.SetBool ("Running", false);
if (Input.GetKey ("left shift")){
vect.x = moveSpeed * 2.0f;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", false);
animator.SetBool ("Running", true);
}
} else if (Input.GetKey ("a")) {
vect.x = moveSpeed * -1.0f;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", true);
animator.SetBool ("Jumping", false);
animator.SetBool ("Running", false);
if (Input.GetKey ("left shift")){
vect.x = moveSpeed * -2.0f;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", false);
animator.SetBool ("Running", true);
}
} else {
vect.x = 0.0f;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", true);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", false);
animator.SetBool ("Running", false);
}
if (Input.GetKeyDown ("w")) {
int layerMask = 1 << 8;
layerMask = ~layerMask;
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 1.0f, layerMask);
if (hit.collider != null){
vect.y = jumpPower;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", true);
animator.SetBool ("Running", false);
}
RaycastHit2D hit2 = Physics2D.Raycast(transform.position, -Vector2.right, 1.0f, layerMask);
if (hit2.collider != null){
vect.y = jumpPower;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", true);
animator.SetBool ("Running", false);
}
RaycastHit2D hit1 = Physics2D.Raycast(transform.position, Vector2.right, 1.0f, layerMask);
if (hit1.collider != null){
vect.y = jumpPower;
GetComponent<Rigidbody2D> ().velocity = vect;
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", false);
animator.SetBool ("Running", false);
}
}
}
void SpriteChange (){
Vector2 face = GetComponent<Transform>().transform.localScale;
if (Input.GetKeyDown ("a")) {
if (!faceLeft){
face.x *= -1;
faceLeft = true;
GetComponent<Transform>().transform.localScale = face;
}
}
if (Input.GetKeyDown ("d")) {
if (faceLeft){
face.x *= -1;
faceLeft = false;
GetComponent<Transform>().transform.localScale = face;
}
}
int layerMask = 1 << 8;
layerMask = ~layerMask;
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 0.5f, layerMask);
if (!hit.collider) {
animator.SetBool ("Standing", false);
animator.SetBool ("Walking", false);
animator.SetBool ("Jumping", true);
animator.SetBool ("Running", false);
}
}
void Respawn(){
if (rb.position.y <= -3) {
gameObject.transform.position = respawnPoint.position;
}
}
void Update () {
MoveAnimate ();
SpriteChange ();
Respawn ();
}
}
Answer by Mulldor · Jul 05, 2015 at 09:11 PM
All you need to do is decide when the level is supposed to be completed. Such as when you have reached X amount in score. Or when as you said, you collide with a flag.
If we use the case where you collide with the flag, you use the collider to detect that the player is colliding with desired object. Then you either just change the level, or you play a video (if you have cutscenes). But for simplicity lets just change level.
Put this in a script by itself, and put onto the desired finishing object. So you can reuse it for other projects without having to enter any code.
void OnTriggerEnter (Collider coll)
{
if(coll.tag == "player")
{
Application.LoadLevel("nameOfNextLevel");
}
}
Instead of using Trigger you can ofc use OnCollision instead.
You can also change coll.tag to anything such as coll.name, if you want the goal to be name specific.
P.S
If you would like to use the code in the script you are using above. Just change the coll.tag to whatever tag you use for finishing the game.
Yep, it worked! Thank you for the help. If I may make one correction, though, the tag "Player" is, by default, spelled with a capital P. Took me a few tries to get it, but it works. Thanks again!