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 BakeSmash · Oct 09, 2019 at 07:24 AM · 2d-platformerladder

Unity2D - Creating multiple Ladders that centers the player?

I am trying to create a ladder that will lock the X axis and center the player. The code I have been working towards, below, accomplishes this... but this doesn't expand to more than one ladder. I can't get the player to center.. it just teleports the player to "Ladder". I understand why, now, but my attempts at altering the script ends in failure :/

I would like to be able to drag and drop my ladder prefab into a scene and have it work with the code. When I try to do FindGameObjectsWithTag.. it doesn't work because apparently it's an array?

I am new to Unity and C#, obviously, but this has been tripping me up for days. I have watched many videos and read many articles.. but I cannot put this together.

Thanks in advance to whomever can assist :) See code below (minus all the extra script)

public GameObject Ladder;

void FixedUpdate() {

Ladder = GameObject.Find("Ladder");

    RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.up * 2, distance, whatIsLadder);

     if (hitInfo.collider != null)
     {
         if (Input.GetKey(KeyCode.W) || (Input.GetKey(KeyCode.S)) && isGrounded == false)
         {
             isClimbing = true;
         }
     }
     else
     {
         isClimbing = false;
     }

     if (isClimbing == true)
     {
         r_anim.SetBool("isClimbing", true);
         inputVertical = Input.GetAxis("Vertical");
         rb.velocity = new Vector2(0, inputVertical * climbSpeed);
         rb.gravityScale = 0;
         //center on ladder
         Vector2 center = new Vector2(Ladder.transform.position.x - .1f, 0);
         rb.transform.position = new Vector3(center.x, rb.transform.position.y);

     }
     else
     {
         r_anim.SetBool("isClimbing", false);
         rb.gravityScale = 10;
     }
     //if climbing and become grounded, stop climbing
     if (isClimbing)
     {
         if (isGrounded == true)
         {
             isClimbing = 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Oct 09, 2019 at 11:05 AM

Ooookay


hey there and welcome to the forum,


first off, try to minimize as much code as possible in a fixed update. This is way too much physics unrealted stuff going on there.


Secondly, do NEVER EVER use general find functions like Gameobject.Find or FindObjectOfType in an Update or FixedUpdate. If you take a look at the profiler this is horrible on performance.


Next: Your problem is probably that GameObject.Find("Ladder") will return A Gameobject with this name. But no specific one. So if there are more than one with this name there will be one ladder that will always be the first. You should get the ladder gameobject from your raycast output.

Something like Ladder = hitInfo.gameobject; should be your ladder. Then it should work as intended.


In general it can sometimes help to read the Documentation on this. It will point out all the stuff that i mentioned in my post.

Hope this helps, let me know if something was unclear. Happy coding :)

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

121 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect CompositeCollider2D shape center? 0 Answers

Physics2D checking if object goes out of OverlapArea 1 Answer

climb speed changes drastically when game window is not maximized 1 Answer

2d Ladders snap to middle? 1 Answer

2D Ladder Climb Through Platforms 2 Answers


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