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 ryanzec · Dec 05, 2016 at 07:33 AM · 2dphysics2d

rigidbiody2D.velocity weirdness

So I was trying to follow some code I saw for a top down 2d character movement and wrote this:

 rigidbody2D.velocity = new Vector2(
     Mathf.Lerp(0, Input.GetAxisRaw("Horizontal") * speedModifier, 1f),
     Mathf.Lerp(0, Input.GetAxisRaw("Vertical") * speedModifier, 1f)
 );

However when I do that I get the error of:

Type UnityEngine.Component does not contain a definition for velocity and no extension method velocity of type UnityEngine.Component could be found. Are you missing an assembly reference?

So I figured it was just a version change so I did the same thing I did for getting the Animator and added private Rigidbody2D rigidbody2D; and rigidbody2D = GetComponent<Rigidbody2D>(); to the Start() method and while that makes the code work, I get this waring:

PlayerController.rigidbody2D hides inherited member UnityEngine.Component.rigidbody2D. Use the new keyword if hiding was intended

What am I doing wrong? What is the correct way to access the Rigidbody2D Component without getting a warning / error and have the code work?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ryanzec · Dec 11, 2016 at 11:24 PM

From what I have gathered, it seems like rigidbody2D is an automatically created property in the script however it is not preferred to use GetComponent but the old property is still there so tried to override it causes the warning. To get rid of the warning and use the GetComponent way of getting the rigidbody2D I just needed to add new to the property definition like new private Rigidbody2D rigidbody2D;.

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

Answer by Paricus · Dec 05, 2016 at 03:17 PM

All your code seems correct. can u send the entire script

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

Answer by Adriansuzr · Aug 10, 2017 at 05:23 PM

I used GetComponent<Rigidbody2D>() and that worked for me but I don't know if that would work for you too. I'm just a beginner.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to walk on 2d sphere (planet) 2 Answers

Triying to make a character take knockback in a parabolic motion in 2D. 0 Answers

Unity 5 2D platformer jumping problem after porting 0 Answers

2D Raycasting to check if grounded not working 1 Answer

how to jump at fixed height but faster 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