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 dhood2 · Apr 09, 2018 at 05:58 PM · 2dreflection

Why does my reflect code not work?

alt text

in the attached photo is my movement and reflection code, the movement works fine, however the reflect script is not working and I cannot figure out why. I have a 2d circle with a circle collider as the "in" object, and a rectangle with a box collider as the reflector, any help is appreciated.

snipperrr.png (31.6 kB)
Comment
Add comment · Show 1
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 TreyH · Apr 09, 2018 at 06:09 PM 2
Share

Can you elaborate on "not working"? What is it doing ins$$anonymous$$d?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Chimer0s · Apr 09, 2018 at 08:54 PM

It's the line "inDirection = newVelocity;" at the end. This should be "GetComponent().velocity = newVelocity;"

When you declare inDirection earlier in the code, you're actually retrieving the velocity value and storing it as a vector2. Changing the vector2 doesn't automatically change the source, it only changes that stored value.

Comment
Add comment · Show 9 · 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 dhood2 · Apr 10, 2018 at 04:54 PM 0
Share

So I tried replacing the line with GetComponent().velocity and it wanted more information so I specified that it was the rigidbody velocity that I was looking for (same way I got it for inDirection which resolved the error however the ball is still not correctly reflecting.

avatar image Scribe dhood2 · Apr 10, 2018 at 06:09 PM 0
Share

Have you tried debugging the value of velocity at this point, I'm not certain of the order of things but perhaps velocity has already been set to zero or another value by Unity physics? You could perhaps try using coll.relativeVelocity ins$$anonymous$$d.

Could you explain more about what the current incorrect behaviour is.

avatar image dhood2 Scribe · Apr 10, 2018 at 06:29 PM 0
Share

well, when it hits the wall it kinda just rocks back and forth on the wall, I can get it to reflect if I multiply the newVelocity by more than 4, but it is a weird deflection.

Show more comments
avatar image TreyH dhood2 · Apr 10, 2018 at 06:17 PM 0
Share

What do you mean that it wanted more information? It complained until you gave used this.GetComponent<Rigidbody>() ?

avatar image dhood2 TreyH · Apr 10, 2018 at 06:26 PM 0
Share

CS0411 The type arguments for method 'Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly. I get this error when I don't specify the rigidbody2d

avatar image Chimer0s dhood2 · Apr 17, 2018 at 12:48 PM 0
Share

That was a typo on my part. Sorry. It should be GetComponent().velocity as you figured.

avatar image dhood2 Chimer0s · Apr 17, 2018 at 05:54 PM 0
Share

your original post does have GetComponent().velocity = newVelocity, I don't see the typo. Good ti$$anonymous$$g btw I had someone write me a code that seemed to work for a while but it had large issues that I couldn't work around.

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

160 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 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

Grabpass 2D water reflection, UV issue 0 Answers

How do i make something reflect in 2D? 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

How do I get reflection vector from a 2D collision 1 Answer

2D - more lights 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