Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 05, 2015 at 10:33 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by 923penguin · Jul 05, 2015 at 08:58 PM · 2dcollidertriggerplatformer

[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 ();
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 923penguin · Jul 05, 2015 at 10:09 PM 0
Share

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!

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider2D/RigidBody2D not working 1 Answer

Triggers Interacting with Triggers 0 Answers

Triggering platform animation on colliding with Button 0 Answers

Collider trigger only affecting a single game object when it should affect multiple 0 Answers

Trigger only one collider? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges