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 KOemxe · Aug 30, 2018 at 05:00 AM · gravitycubenormalssurfacepipe

Move along surface normal of hollowed pipe with cube

Hi all,

I love @Marsupilami 's script here in another thread - https://answers.unity.com/questions/545844/make-player-character-stick-to-the-level-mesh-and.html - it's allowing me to starting moving my player cube around a pipe wall much of the time!

One drawback for me though - I think part of my problem is that I have to allow all sides of the cube to be the normal to the surface. Here's my movement code:

 // Update is called once per frame use fixed update for Unity Fizzix
     void FixedUpdate () {
         //distanceFromPipeCenter.Normalize();
         //add forward force
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
         
         if (Input.GetKey(KeyCode.RightArrow) /*&& !fauxGravity*/)
         {
             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         }
         
         if (Input.GetKey(KeyCode.LeftArrow) /*&& !fauxGravity*/)
         {
             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         }
         //if (Input.GetKey(KeyCode.RightArrow) && fauxGravity)
         //{
         //    //Debug.Log("Right pressed");
         //    rb.AddForce(sidewaysForce * pipePull.x*Time.deltaTime, sidewaysForce * pipePull.y * Time.deltaTime, 0, ForceMode.VelocityChange);
         //    Debug.Log(rb.angularVelocity);
         //    float headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * sidewaysForce;
         //    Quaternion headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
         //    //align with surface normal
         //    transform.rotation = Quaternion.FromToRotation(transform.up, distanceFromPipeCenter) * transform.rotation;
         //    //apply heading rotation
         //    transform.rotation = headingDelta * transform.rotation;
         //}
         //if (Input.GetKey(KeyCode.LeftArrow) && fauxGravity)
         //{
         //    //Debug.Log("Left pressed");
         //    rb.AddForce(-sidewaysForce * pipePull.x * Time.deltaTime, -sidewaysForce * pipePull.y * Time.deltaTime, 0, ForceMode.VelocityChange);
         //    Debug.Log(rb.angularVelocity);
         //    float headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * sidewaysForce;
         //    Quaternion headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
         //    //align with surface normal
         //    transform.rotation = Quaternion.FromToRotation(transform.up, distanceFromPipeCenter) * transform.rotation;
         //    //apply heading rotation
         //    transform.rotation = headingDelta * transform.rotation;
         //}
         if (fauxGravity)
         {
             float distForward = Mathf.Infinity;
             RaycastHit hitForward;
             
             if (Physics.SphereCast(transform.position, 0.25f, -transform.up + transform.forward, out hitForward, 5))
             {
                 distForward = hitForward.distance;
             }
             float distDown = Mathf.Infinity;
             RaycastHit hitDown;
             if (Physics.SphereCast(transform.position, 0.25f, -transform.up, out hitDown, 5))
             {
                 distDown = hitDown.distance;
             }
             float distBack = Mathf.Infinity;
             RaycastHit hitBack;
             if (Physics.SphereCast(transform.position, 0.25f, -transform.up + -transform.forward, out hitBack, 5))
             {
                 distBack = hitBack.distance;
             }
 
             if (distForward < distDown && distForward < distBack)
             {
                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitForward.normal), hitForward.normal), Time.deltaTime * 5.0f);
             }
             else if (distDown < distForward && distDown < distBack)
             {
                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitDown.normal), hitDown.normal), Time.deltaTime * 5.0f);
             }
             else if (distBack < distForward && distBack < distDown)
             {
                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitBack.normal), hitBack.normal), Time.deltaTime * 5.0f);
             }
 
             GetComponent<Rigidbody>().AddForce(-transform.up * Time.deltaTime * 10);
         }
         if (rb.position.y <-1f)
         {
             FindObjectOfType<GameManagement>().EndGame();
         }
     }

You can see in my extra block of commented code, I've tried using an alternate method to detect when the player has collided with a hollowed pipe object (it starts moving inside the object). I want the cube to be attracted to the wall with a gravitational force relative to the surface of the pipe.

Any ideas?

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

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

91 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

Related Questions

Simulating magnetic shoes? 0 Answers

Object goes through wall. 0 Answers

Drawing a cube by a new Mesh 2 Answers

Normal Walker: Player Y Rotation Snapping back to 0? 0 Answers

Moving the cube on a surface. 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