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

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

After added a rigidbody in a GameObject, it moves on rotation 2 Answers

Is it possible to change gravity for only one object? 1 Answer

Rigidbody player movement doesn't response to gravity 0 Answers

let rigidbody idle around in space 1 Answer

Allowing player to glide/fall slowly 2 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