• 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 jazzaling · Dec 13, 2019 at 10:13 AM · rigidbodygravityflying

still got momentum after "Use Gravity" On rigidbody is turned off

I want my player to be able to switch between walking and flying fluently. But after i turn off "Use gravity" on the rigidbody the player has still got the momentum, i was wondering if there is anyway around this? theres probably a easy fix but i just havent seen it yet.

 public float moveSpeed = 15f;

 public float flySpeed = 10f;
 public float velocitySpeed = 0f;

 public float jumpHeight = 5f;

 private float rayHeight = 1.1f;//ray is raycast down towards the ground, to check if player grounded
 
 private bool isGrounded;
 public bool isFlying;//testing to see if the player is walking or flying


 private Rigidbody rigid;



 // Start is called before the first frame update
 void Start()
 {
     rigid = gameObject.GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update()
 {

     int layerMask = 1 << 9;
     layerMask = 9;

     RaycastHit hit;
     Ray ray = new Ray(transform.position, -transform.up);
     Physics.Raycast(ray, out hit, rayHeight, layerMask);

     Debug.DrawRay(ray.origin, ray.direction * rayHeight, Color.red);

     if (hit.collider)
     {
         Debug.Log("Hitting Collider");
         isGrounded = true;
         isFlying = false;
     }
     else if (!hit.collider)
         isGrounded = false;


     if (isGrounded)
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rigid.AddRelativeForce(new Vector3(0,jumpHeight), ForceMode.Impulse);
             
         }
     }

     if (!isGrounded)
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
                isFlying = true;
         }
     }

     if (!isFlying)
     {
         rigid.useGravity = false;

         float translationZ = Input.GetAxis("Horizontal") * moveSpeed;
         float translationX = Input.GetAxis("Vertical") * moveSpeed;

         translationZ *= Time.deltaTime;
         translationX *= Time.deltaTime;


         transform.Translate(translationX, 0, translationZ);
     }


     if (isFlying)
     {
         rigid.useGravity = true;

         velocitySpeed = Mathf.Clamp(velocitySpeed, -0.1f, 0.1f);

         float translationZ = Input.GetAxis("Horizontal") * flySpeed;
         float translationX = Input.GetAxis("Vertical") * flySpeed;
         float translationY = Input.GetAxis("Jump") * +velocitySpeed * Time.deltaTime;

         translationZ *= Time.deltaTime;
         translationX *= Time.deltaTime;

         bool increaseHeight;
         
         if (Input.GetKeyDown(KeyCode.Space))
         {
             increaseHeight = true;
            
             if (increaseHeight)
             {
                 velocitySpeed += 0.1f;
             }
         }
         if (Input.GetKeyUp(KeyCode.Space))
         {
             increaseHeight = false;
             if (!increaseHeight){
                 velocitySpeed = 0f;
             }
         }

         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
             increaseHeight = true;

             if (increaseHeight)
             {
                 velocitySpeed -= 0.1f;
             }

         }
         if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             increaseHeight = false;

             if (!increaseHeight)
             {
                 velocitySpeed = 0f;
             }

         }
         transform.Translate(translationX, velocitySpeed, translationZ);




     }

 

 }

}

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 lgarczyn · Dec 13, 2019 at 07:36 PM

You shouldn't use Translate on rigidbodies.

You should only use MovePosition on a kinematic rigidbody. You should only set the velocity or use AddForce on a dynamic (non-kinematic) rigidbody. (There are exceptions, but that's the gist of it.)

The problem is that right now you use both transform-based movement and physics based movement.


To answer your question, you can just reset the momentum or velocity like this:

 rigidbody.velocity = Vector3.zero;



However you need to chose between a kinematic rigidbody, dynamic rigidbody, or CharacterController.

I advise that you use a CharacterController, and use Move to move it around. You can replace all your calls to Translate by Move. Copy this script to apply gravity when not flying.

A kinematic rigidbody will need a Raycast to check for collisions, but it won't be affected by the rest of the world.

A dynamic rigidbody has access to velocity, and therefore can have useGravity, but it might get pushed around by other moving objects.

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 jazzaling · Dec 14, 2019 at 03:03 AM 1
Share

Hey cheers for that my man, appreciate it <3

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

237 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable gravity (freeze character in mid air) without him flying into space? 1 Answer

Rigidbody.AddForce seems not to do anything 0 Answers

wing suit physics 1 Answer

GravityBody hides Rigidbody 1 Answer

Rigidbody doesn't work with my script 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges