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 mixmax2 · Oct 13, 2014 at 04:57 AM · movementphysicscollidercharacter

How to prevent rigidbody with capsule collider from walking off the edge of cube mesh if button held down?

I can't seem to figure out how to do this within Unity.

This kind of mechanic can be seen in Minecraft when holding down left shift, so that the player can place blocks adjacent to one he is standing on, by basically hovering over the very edge of the block.

Edit #1: Movement Script Info (I put it here in case it is somehow relevant, but I don't need an implementation that works with my current movement system, just the idea of how to do the mechanic):

I use a raycast to determine if the player is grounded, then I use a slightly modified wiki script for movement:

             float horizontal = Input.GetAxis("Horizontal");
             float vertical = Input.GetAxis("Vertical");
             float percentofpercent = Mathf.Abs(horizontal) + Mathf.Abs(vertical) - 1;
             if (percentofpercent > 0.1f)
             {
                 percentofpercent = percentofpercent * 10000;
                 percentofpercent = Mathf.Sqrt(percentofpercent);
                 percentofpercent = percentofpercent / 100;
                 float finalMultiplier = percentofpercent * .25f;
                 horizontal = horizontal - (horizontal * finalMultiplier);
                 vertical = vertical - (vertical * finalMultiplier);
             }
             
             if (vertical > 0) {
                 vertical = vertical * forwardSpeed;
             }
             else if (vertical < 0) {
                 vertical = vertical * backwardSpeed;
             }
             
             horizontal = horizontal * strafeSpeed;
             
             Vector3 vel = transform.TransformDirection(new Vector3(horizontal, rigidbody.velocity.y, vertical));
 
             rigidbody.AddForce(new Vector3(vel.x - rigidbody.velocity.x, 0, vel.z - rigidbody.velocity.z), ForceMode.VelocityChange);

Edit #2: What I am talking about is the mechanic prevents you from being able to fall off of the edge of a block, it acts as if there is a wall an arbitrary distance away for every open edge of that current block the player is on, while also letting you hover an arbitrary distance off of that edge that normally would cause you to fall down (so that you can look at the side of that block without falling off), and this is true for any blocks you happen to walk against on the same plane, and connected to that initial block.

My only thought was to create a collision mesh on top of the block that basically had 4 walls and a floor that had dimensions larger than the block the player was currently standing on, and constantly checking for which block the player was on top of in order to reposition this mesh. But I really hate the thought of it, and was wondering if anyone had implemented this mechanic before and had a more elegant solution.

alt text

In the multicolored example, those individual collision meshes would only exist for each block when that player is on/near it and the key is held down. Again, I despise the idea of doing it in such a backwards way, and was hoping someone had implemented this before or knew how Minecraft did it, before I go ahead and do this hacky approach.

I also can't just give the player a box collider with a larger area, because that wouldn't work if the player is completely surrounded by walls on 3 edges of the block he is on, and also, while a bigger box collider will allow the player to go farther over an edge, it will do nothing to stop him from falling off.

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 mixmax2 · Oct 13, 2014 at 02:35 AM 0
Share

I'm not specifically looking for an answer that works with my movement system, although I did edit my question to include my basic movement script.

I am looking to understand what kind of different solutions there are to this problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Oct 14, 2014 at 08:08 AM

All you would have to do is change the capsule collider to a box collider, then lock the rotation on the vertical axis' via the rigidbody component. If you wanted this only to happen on holding a button down, you could add both the capsule collider and the box collider & configure them, then have a script something like this:

 #pragma strict
 
 var bCol : BoxCollider;            //Box collider
 var cCol : CapsuleCollider;        //Capsule collider
 
 function Start () {
     bCol = gameObject.GetComponent(BoxCollider);
     cCol = gameObject.GetComponent(CapsuleCollider);
 }
 
 function Update () {
     if (Input.GetKey(KeyCode.LeftShift)){        //If you wanted this to happen when pushing left shift, like in your example
         bCol.enabled = true;                    //Disable/Enable the selected colliders
         cCol.enabled = false;
     } else {
         bCol.enabled = false;                    //Swap the first Disable/Enable
         cCol.enabled = true;
     }
 }

You could implement your script with this or vice/versa. To be perfectly honest, I'm not 100% sure what you are trying to accomplish, this is purely going off the given example, but I hope it helps, Namey5.

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 mixmax2 · Oct 14, 2014 at 10:17 AM 0
Share

*Updated my question with more information ins$$anonymous$$d of putting the block of text here. See Edit #2.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers

Stopping my player from moving when hitting a wall. 5 Answers

Powered Ragdoll? Direct Input for a Rigged Character 2 Answers

How to connect two spheres one to stay on top of another which is rotating moveing one (player body , head) 0 Answers

Character controller jittering up and down 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