- Home /
How not to make a RigidBody not go into ground when tilted foward?
I have a game object (RigidBody) that levitates when moving forward. I 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.
I want to know how it would be possible to make the object continue moving forward and levitating while tilting and not run into the ground.
How are you moving the GameObject?
What direction are you moving it in?
Does the Rigidbody still apply global gravity when it is levitating?
It would be very helpful if you could offer some more information and some relevant samples of your script so we can do more than speculate what's going on when you move the GameObject around.
Answer by darkStar27 · Dec 14, 2018 at 03:54 AM
If its falling to the ground.:
Check weather the Use Gravity field is not selected.
If that's not the case, and it should collide with the ground than use a collider on the rigidbody as well as the ground.
if you trying to ask something else then explain the problem further.
Hey guys. Thanks for your replies. Sorry for not getting back to you sooner: we took a break for the holidays.
Here is our current code, which we help will make it easier for you to understand our current scenario:
private void $$anonymous$$ovingForward()
{
Vector3 movement = transform.forward * m_$$anonymous$$ovementInputValue * m_Speed * Time.deltaTime;
rb.position += movement;
rb.$$anonymous$$ovePosition(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.$$anonymous$$ovePosition(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 ("$$anonymous$$ove 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 ins$$anonymous$$d. See the attached picture.
Eventually we would want this to work with a non-level ground: that is, going up a hill, etc.
Thanks!
Your answer
Follow this Question
Related Questions
How to make a RigidBody not go into ground when tilted foward? 2 Answers
Prevent Rigidbody From Rotating 3 Answers
CONTROL RIGIDBODY MOVEMENT 2 Answers
Rotating my character only while moving in a 2D plane 3 Answers
Keep Horizontal Momentum after Jump 2 Answers