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 camomatic · Sep 22, 2014 at 11:01 AM · charactercontrollercapsulecast

Capsule casting for custom character controller

I am making a game with Mario Galaxy style physics, using capsule casting to snap to the surface of the planet. I'm not sure whether it is my use of CapsuleCast or another part of the controller that is causing this so I am try to post relevant bits of code. The script is run from a player object, which has child containing all the graphics. What I want to happen is for the the distance to the ground to be calculated from the CapsuleCast and for one of two things to then happen: 1. Within a certain distance to the ground, automatically move to contact 2. If the ground is further away, go into free fall. (I haven't got this far yet)

Here is the capsule cast code:

         //Check for stuff below us
         capsuleTop = transform.position - transform.TransformDirection(Vector3.up * capsuleHeight * 0.5);
         capsuleBottom = transform.position + transform.TransformDirection(Vector3.up * capsuleHeight * 0.5);
         if (Physics.CapsuleCast(capsuleTop, capsuleBottom, capsuleRadius, transform.TransformDirection(Vector3.up), hitInfo, maxGroundSnapDist + (capsuleHeight * 0.5))) {
             //If we're close--move to contact //NEEDS DEBUGGING--> WHY DO I SINK INTO THE FLOOR?
             if (hitInfo.distance * 2 > capsuleHeight) {
                 transform.Translate(Vector3.ClampMagnitude(hitInfo.point - transform.position, hitInfo.distance * 0.5), Space.World);
                  grounded = true;
              }
         }
         else { //If we're not close--freefall
             grounded = false;
             //Freefall coming soon
         }

Here is the code that aligns the player to the surface normal:

 function AlignToSurface () {
     //Align to surface normal
     transform.rotation = Quaternion.FromToRotation(transform.up, (gravitySource - transform.position.normalized)) * transform.rotation;
     //(all the objects are circular, so alignment based on source of gravity instead) 
 }

And the code that rotates the graphics to the current direction of travel:

 function RotateGraphics () {
     var angleDifference = AngleSigned(graphics.InverseTransformDirection((previousPos - transform.position).normalized), -Vector3.forward, -Vector3.up);
     if (angleDifference > 90) {
         angleDifference -= 360; //A little angle fudging
     }
     var lerped = Mathf.LerpAngle(0, angleDifference, 0.1); //Smooth rotation
     graphics.Rotate(0, lerped, 0);
 }    

However two things happen: ONE. I don't snap to the surface alt text

TWO. If I run at full speed, the player works fine. Just staying at the original height above the planet. However if I move in little steps, by tapping the keys. The player sinks into the ground. BUT it is only the graphics object that sinks!! As far as I can tell, the parent stays in the same place. The graphics object is only modified in the scripts I have uploaded.

alt text

Can anyone tell me what I'm doing wrong? Or know a better way of doing it?

Thanks

before.jpg (45.9 kB)
after copy.jpg (20.9 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 camomatic · Sep 22, 2014 at 01:19 PM 0
Share

I have solved the sinking problem by brute force, by just setting the graphics' localPosition to zero every frame. Seems to do the trick though :)

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

CapsuleCast not registering.... 3 Answers

Spherecast/Capsulecast bug/glitch 1 Answer

How to fix the position of a character when grabbing a rope? 1 Answer

Help! Problems with Character.isGrounded in Unity 4.0.1? 3 Answers

what are realistic values for the character controller 1 Answer


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