Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 senthereborn · Sep 06, 2012 at 11:08 AM · collisioncollidermovetowardsclippingwall

Stop Dashing through walls

Hey guys. I'm working on a sidescrolling platformer and I have a GameObject with a box collider and a rigid body that I want to dash to a desired location using the following code -

 while(Vector3.Distance(Vector3.Distance(transform.position, targetLocation) > 0.1 && timeOfDash < 1)
         {
             timeOfDash += Time.deltaTime;
             transform.position =      Vector3.MoveTowards(transform.position,targetLocation,Time.deltaTime*dashSpeed);
             yield return null;
         }

Everything seems to be working fine until it comes down to collision. If my Object is to dash to a location and is blocked by the wall, it looks as if the floor and the code are fighting with eachother, throwing my game object back and forth (clipping through the wall), until my dash times out (timeOfDash >= 1 ) and the floor is throwing my object upwards.

Is there a way to get perfect collision (so nothing can ever clip through, no matter the code)? Or maybe Vector3.MoveTowards is a bad solution to my problem?

Please advice.

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
1
Best Answer

Answer by jaskij · Sep 06, 2012 at 01:29 PM

Do not move the object by means of moving the transform. Instead, modify the speed of the rigidbody (that should be done in the FixedUpdate() method inherited from MonoBehaviour). Preferably by using Rigidbody.Addforce(), although modifying the speed vector directly is also possible should you want to do so (look into Vector3 Rigidboy.velocity).

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 senthereborn · Sep 06, 2012 at 01:32 PM 0
Share

Ah yes! Haven't tried that out yet, but it sounds like the correct solution. Adjusting a "Speed modifier" does sound like a much cleaner way than moving the transform directly. I'll check it out and tick you up when I'm done :)

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

making bullets disappear when they collide 3 Answers

Why is my AI not thinking the collisions fast enough? 1 Answer

Can mesh colliders be used for buildings? 1 Answer

Colliders and GetKey 0 Answers

How to destroy the cube which I selected with the arrow key 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