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 /
  • Help Room /
avatar image
0
Question by JayArtist · Dec 04, 2015 at 01:48 PM · c#rigidbody2dmovement scriptplatformergrounded

How to tidy up my code for Unity 5 Basic Platformer

Hi guys,

I've been following a recent tutorial to set up a basic platformer. The script works, but it's a little untidy. So I thought I'd do the typical suggest to change the lengthy:

GetComponent ().velocity = new Vector2 (GetComponent ().velocity.x, jump);

to something shorter and more readable:

rb.velocity = new Vector2 (rb.velocity.x, jump );

But it just isn't working with the changes. Below is the full C# script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     
     //Movement
     public float speed;
     public float jump;
     float moveVelocity;
     public Rigidbody rb;
     bool IsGrounded = true;
 
     void Start() 
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void Update ()
     {
         //Jumping
         if (Input.GetKeyDown (KeyCode.UpArrow) && (IsGrounded == true))
 
         {
             //rb.velocity = new Vector2 (rb.velocity.x, jump );
             GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
         }
         
         moveVelocity = 0;
         
         //Left Right Movement
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             moveVelocity = -speed;
         }
         if (Input.GetKey (KeyCode.RightArrow))
         {
             moveVelocity = speed;
         }
         
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
 
         Debug.Log(IsGrounded);
         
     }
     
     //Check if Grounded
     void OnTriggerEnter2D()
     {
         IsGrounded = true;
     }
     void OnTriggerExit2D()
     {
         IsGrounded = false;
     }
 }

Any help or suggestions how I could improve the script would be appreciated.

Thanks, Jay.

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 Landern · Dec 04, 2015 at 01:59 PM

If you're using Rigidbody2d components on your GameObjects, i would go ahead and change your Start method to actually pull the same component type that is used throughout your code. Variable rb is probably not set to the component you want given the context of the rest of your script.

 // Get Rigidbody2d not Rigidbody
 void Start() 
 {
     rb = GetComponent<Rigidbody2D>();
 }

Now that the right object type matches across the board you should be able to tighten things up.

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 JayArtist · Dec 04, 2015 at 02:03 PM 0
Share

Silly noob mistake, thanks.

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

35 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

Related Questions

Ghange the code of the character controller in 3d platformer game kit 0 Answers

Player sometimes don't stop moving when Key is release, and is properly installed in the fixedUpdate() 1 Answer

How to Change Rigidbody Type OnCollision/Trigger with Script? 1 Answer

Move Player along normal of floor? 1 Answer

2d Rigidbody how to check if Player is moving towards the mouse 0 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