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 /
This question was closed May 13, 2014 at 12:07 PM by Cerbion_ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Cerbion_ · Feb 01, 2014 at 12:39 AM · c#movementcollidervelocity

Prevent Object/Character from "drifting" away from Collider

Hey folks!

I am currently working on a 2D MMO RPG!

And I am sort of running into a little "problem"..

Here's the thing: I have a gameobject (the Player) who is moved using the transform.Translate function, everything works fine and all, but if a player runs against a collider which stops the player from moving (a simple box collider) the collider also creates velocity so that the player starts to "drift" away from the colliderbox... Now I do use Sleep() and WakeUp() when the player lets go of all the movement buttons, so the player stands still again, which works fine, but it doesn't solve the problem if the player keeps holding a movement key after running against a wall (which can make a player significantly faster..)

If you need any exact codelines, just tell me, and I shall add them!

Cerbion

Comment
Add comment · Show 4
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 getyour411 · Feb 01, 2014 at 12:53 AM 0
Share

Does your player and collider have a parent/child relationship or is it all one contained GameObject?

avatar image Cerbion_ · Feb 01, 2014 at 01:25 AM 0
Share

Well! (I had to check on this one ._.)

The Character consists of a couple of Game objects, but the main one (the parent object) has both the Collider and the $$anonymous$$ovement Script attached to it!

// EDIT: screenshot: http://puu.sh/6Fw72.png

avatar image getyour411 · Feb 01, 2014 at 01:40 AM 0
Share

I think misunderstood you. I thought you were saying the player and its simple collider are 'drifting' away from each other. You are actually saying when this player hits a different collider (wall) he gains some kind of acceleration bonus. Is that right?

If so, could you post the relevant code on the object collider that's adding velocity?

avatar image Cerbion_ · Feb 01, 2014 at 03:08 AM 0
Share

Sorry if I my writing led to this misunderstanding Q_Q english is not my native language :I

Anyways! Here you go:

 // VERTICAL AND HORIZONTAL $$anonymous$$OVE$$anonymous$$ENT
             if(moving == "left") // $$anonymous$$ove Character LEFT
             {
                 velocity = new Vector3(1 / acceleration_normal, 0 , 0);
                 $$anonymous$$ovePlayer("left");
             }
             if(moving == "right") // $$anonymous$$ove Character RIGHT
             {
                 velocity = new Vector3(-1 / acceleration_normal, 0 , 0);
                 $$anonymous$$ovePlayer("right");
             }
             if(moving == "up") // $$anonymous$$ove Character UP
             {
                 velocity = new Vector3(0, 0 , -1 / acceleration_normal);
                 $$anonymous$$ovePlayer("up");
             }
             if(moving == "down") //$$anonymous$$ove Character DOWN
             {
                 velocity = new Vector3(0, 0 , 1 / acceleration_normal);
                 $$anonymous$$ovePlayer("down");
             }
 
             if(moving == "stop")
             {
                 velocity = Vector3.zero;
                 $$anonymous$$ovePlayer("stop");
             }
 
             if(velocity != Vector3.zero)
             {
                 transform.Translate(velocity);
             }
             else
             {
                 transform.rigidbody.Sleep();
             }

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Cerbion_ · May 13, 2014 at 12:07 PM

I know this thread is old, but I fixed it on my own:

I had no Gravity/Drag enabled on my rigidbody (because the game's only 2D) but with the gravity and drag/angular drag enabled and set to a moderate amount, everything works and the character is not getting pushed away from walls anymore!

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

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Using Animation Curve for Movement Help 1 Answer

Carry forward velocity against mouse movements 1 Answer

Problem with objects colliding and IsSleeping (C#) 1 Answer

Return Proper Velocity of Player object 3 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