Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Basse00 · Apr 02, 2017 at 10:03 AM · c#scripting problem2d-platformerclimbingclimb

2d-platformer climb

I'm trying to make a 2d-platformer. In this game I want to make the player be able to climb on ladders. I haven't found a good tutorial that helped me. The console prints out "I'm standing on ground." but I can't climb the ladder. I would appreciate help.

This is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class PlayerMovement : MonoBehaviour
 {
     Rigidbody2D rb2d;

 public float maxSpeed = 5f;
 public bool onLadder = false;

 private void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }

 private void FixedUpdate()
 {

 }

 void Climbing()
 {
     float y = Input.GetAxis("Vertical");

     Vector2 movement = new Vector3(0.0f, y, 0.0f);
     rb2d.velocity = movement.normalized * maxSpeed;
 }

 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Ladder")
     {
         onLadder = true;

         if (onLadder == true && Input.GetKeyDown("w"))
         {
             Climbing();
         }

         Debug.Log("I'm on ladder.");
     }
     else
     {
         onLadder = false;
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Albarnie · Jul 14, 2017 at 02:19 AM

Your problem seems to be that you are only checking if the player is pressing the 'W' key when the player enters the trigger.

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 Albarnie · Jul 14, 2017 at 02:21 AM 0
Share

oops, i forgot to mention you probably want to set onLadder to false on trigger exit

avatar image
0

Answer by Cornelis-de-Jager · Jul 14, 2017 at 03:06 AM

The problem is that OnTriggerEnter2D() only gets called once. Your velocity will be changed, but will quickly drop down again because it only runs once. Change your function to OnTriggerStay2D(). That will check if you are pressing W continuously while inside the trigger.

More Explination:

When working with triggers and colliders there are three states you can possibly have:

  • Enter : When you first enter the trigger or collider (Gets Called only Once/ Resets when you leave)

  • Stay: Runs as long as you are within the trigger or collider

  • Exit: When you leave trigger or collider (Gets Called Only Once / Resets when you enter again)

However, a different way of doing it is attaching the following script to the ladded object:

 using UnityEngine;
 using System.Collections;
 
 public class ladder () {
   
   OnTriggerEnter2D (Collider other) {
    // Stops the player from being affected by gravity while on ladder
     if (other.Tag == "Player")
       other.gameObject.GetComponent<RigidBody2D> ().gravityScale = 0;
  }
 
 OnTriggerStay2D (Collider other) {
   if(!(other == "Player"))
     return;
 
   float y = Input.GetAxis ("Vertical");
        other.transform.translate (new Vector3 (0, other, 0));
 }
 
 OnTriggerExit2D (Collider other) {
    // Stops the player from being affected by gravity while on ladder
     if (other.Tag == "Player")
       other.gameObject.GetComponent<RigidBody2D> ().gravityScale = 1;
  }
 }


Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

How do I make it so it spawns 1 clone instead of 2? 2 Answers

Change 2D box collider Y values but how offset stay on same position and adjust their values. 0 Answers

Problem with converting touch position to world space 0 Answers

Smoothly move 2D Sprite 2 Answers

I cant get wall jumping to work in my 2D platformer (C#) 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