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 /
  • Help Room /
avatar image
0
Question by Dorscherl · Dec 28, 2018 at 05:46 AM · rigidbodyvelocitygravityfallingfsm

Detect if falling with custom gravity

So I'm working on a state machine with a BUNCH of states and I'm trying to determine when the player enters the falling state. I see a bunch of examples that follow something like this: yVel = rigidbody.velocity.y; //y velocity of rigidbody

 if (yVel < 0)    //if it's negative, that means it's falling down so set to true
       isFalling = true;

That would likely work just fine IF i wasn't using a custom gravity script. The script applies a gravitational force to the rigidbody in the direction of gravity.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class GravityBody : MonoBehaviour
 {
     //override the monobehavior's rigidbody getter
     private new Rigidbody rigidbody;
 
     [SerializeField]
     private float m_Acceleration;
     [SerializeField]
     private Vector3 m_GravityDirection = new Vector3(0,1,0);
 
     public bool useGravity = true;
     public bool useFixedUpdate = true; //basically allows us to apply gravity in this script (true) or apply it in a class that uses it
     public float jumpForce = 10;
 
     /// <summary>The acceleration rate of gravity.</summary>
     public float Acceleration
     {
         get
         {
             return m_Acceleration;
         }
         set
         {
             m_Acceleration = Mathf.Min(value, 0);
         }
     }
 
     /// <summary>The direction of gravity.</summary>
     public Vector3 GravityDirection
     {
         get
         {
             return m_GravityDirection;
         }
         set
         {
             m_GravityDirection = value.normalized;
         }
     }
 
     ///<summary>Force applied to body</summary>
     public Vector3 Gravity
     {
         get
         {
             return -Acceleration * GravityDirection.normalized * rigidbody.mass;
         }
     }
 
     public RigidbodyConstraints Constraints
     {
         get
         {
             return rigidbody != null ? rigidbody.constraints : RigidbodyConstraints.None;
         }
         set
         {
             rigidbody.constraints = value;
         }
 
     }
 
     private void Start()
     {
         rigidbody = GetComponent<Rigidbody>();
         rigidbody.useGravity = false;
     }
 
     private void FixedUpdate()
     {
         if (useFixedUpdate)
             ApplyGravity();
 
         //For testing purposes only
         if (Input.GetButtonDown("Jump"))
             rigidbody.AddForce(GravityDirection * jumpForce, ForceMode.Impulse);
 
     }
 
     public void InitGravityBody(Rigidbody rb)
     {
         rigidbody = rb;
         useFixedUpdate = false;
     }
 
     public void ApplyGravity()
     {
         if (useGravity)
         {
             rigidbody.AddForce(Gravity);
         }
     }
 
 
     //Editor
     private void OnValidate()
     {
         m_Acceleration = Mathf.Max(m_Acceleration, 0f);
         m_GravityDirection = m_GravityDirection.normalized;
     }
 
 }
 

This means that won't work in this regards. How do I check if my player is falling in this regards? Is there a cheat I can do to do what I described earlier?

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

1 Reply

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

Answer by Dorscherl · Dec 31, 2018 at 09:05 AM

So I figured it out with some help from the Infallible Code Discord (thanks apieceoffruit!) He told be about Vector3.dots and this is what we came up with

 public bool CheckFalling(bool grounded = false)
     {
         var gravDir = useGravity ? GravityDirection : Vector3.zero;
         FallingVelocity = Vector3.Dot(rigidbody.velocity, gravDir);
 
         if (Mathf.Approximately(FallingVelocity, 0))
             FallingVelocity = 0f;
 
         if ( FallingVelocity < 0 && grounded == false)
         {
             return true;
         }
 
         return false;
     }

So if the character is falling, the dot's value return negative and if the character is ascending it returns positive, all relative to the gravity's direction and velocity of the rigidbody

Comment
Add comment · 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

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

197 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

Related Questions

Using rb.velocity causes low gravity. 2 Answers

How can I make my player jump faster but not higher? 3 Answers

Can only go horizontally while walking on walls, and not vertically 0 Answers

If button is not pressed fall. 2 Answers

Slow falling speed on player (with RB), but all other objects are fine (checked scaling). 0 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