• 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
1
Question by jblanchard15 · Jan 24, 2019 at 12:02 AM · rotationmovementphysicsrigidbodytilting

How to make a RigidBody not go into ground when tilted foward?

We have a game object (RigidBody) that levitates when moving forward. We also make it tilt slightly forward when moving. The issue is that whenever the object is moving with the tilt, it has a tendency of moving into the ground. Here is the code that we currently have:

 private void MovingForward()
  {
      Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
      rb.position += movement;
  
      rb.MovePosition(position: rb.position);
  }
  
  private void LeanForward()
  {
      float lean;
  
      m_leaning = true;
  
      if (m_leanForwardAngle < 20f)
      {
          lean = 1 * 50f * Time.deltaTime;
          m_leanForwardAngle += lean;
          transform.Rotate(Vector3.right * lean, Space.Self);
      }
  }
  
  private void Levitate(int currentState)
  {
      Vector3 levitate = Vector3.zero;
      float levitate_f = 3f;
  
      if (currentState == 0)
      {
          if (m_levitatePressure < m_maxLevitateHeight)
          {
              levitate = transform.up * m_LevitateSpeed * Time.deltaTime;
              m_levitatePressure += m_LevitateSpeed * Time.deltaTime;
              rb.useGravity = false;
              rb.MovePosition(rb.position + levitate);
          }
          else
          {
              m_levitatePressure = m_maxLevitateHeight;
          }
      }
      else
      {
          rb.useGravity = true;
          m_levitatePressure = 0f;
      }
  }

Based on our defined states, our character levitates and tilts slightly when moving forward. You can clearly see what the code is for each action ("Move Forward", "Tilt Forward" and "Levitate"). Let us assume our character is moving to the right and that the ground is a staight line. If we levitate and move forward, our character is moving to the right and levitating properly. However, as soon as we add the tilting, our character's angle with respect to the ground changes and it starts moving into the ground instead. See the attached picture.

alt text

Eventually we would want t$$anonymous$$s to work with a non-level ground: that is, going up a $$anonymous$$ll, etc.

Thanks!

diagram.jpg (90.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by xxmariofer · Jan 24, 2019 at 12:15 AM

Hello, just remove the y value from the transform forward from

 Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;

to

 Vector3 aux = new Vector3(transform.forward.x, 0, transform.forward.z);
 aux.Normalize();
 Vector3 movement = aux * m_MovementInputValue * m_Speed * Time.deltaTime;
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 jblanchard15 · Jan 26, 2019 at 02:54 PM 0
Share
avatar image
-1

Answer by Sktew · Jan 24, 2019 at 08:04 AM

Change:

 Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;

To:

  Vector3 movement = Vector3.forward * m_MovementInputValue * m_Speed * Time.deltaTime;

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 xxmariofer · Jan 24, 2019 at 08:20 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

228 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 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 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

How not to make a RigidBody not go into ground when tilted foward? 1 Answer

Prevent Rigidbody From Rotating 3 Answers

CONTROL RIGIDBODY MOVEMENT 2 Answers

Rotating my character only while moving in a 2D plane 3 Answers

Trying to make a Projectile Rotate and Move Forward.. 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