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 /
avatar image
0
Question by APunkRock · Jun 24, 2019 at 01:09 AM · 2d2d game2d-platformer2d-physicsjumping object

How can I make an object stop all momentum and hold it's position in air?

I'm creating a 2D platform game and I have a character that I want to be able to move and jump as you'd expect in a platformer game, but then on the right surfaces be able to switch to climbing. When climbing they would not be affected by gravity and could move in all directions, not just left/right. I was able to make that work as expected, but I noticed that if the object is falling when I switch to climbing they still fall as if they were being affected by a weaker gravity. What I'd like is for the character to completely stop when the button is hit and no longer have gravity affect them at all.
Below is the script I have currently for this character that can attach to walls.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LizardScript : PlayerCharacter
 {
     public CatScript cat;
     private float climbingSpeed = 7.0f;
     private bool nearWall = false;
     private bool onWall = false;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (GameScript.mainPlayer == "lizard")
         {
             startClimb();
             if (onWall)
             {
                 GetComponent<Rigidbody2D>().gravityScale = 0;
                 climb();
             } else
             {
                 GetComponent<Rigidbody2D>().gravityScale = 2;
                 baseCharacter();
             }
         }
     }
 
     void climb()
     {
         var climbing = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
         transform.position += climbing * climbingSpeed * Time.deltaTime;
     }

     void startClimb()
     {
         if (Input.GetKeyDown(KeyCode.G) && nearWall == true)
         {
             onWall = !onWall;
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.tag == "Wall")
         {
             nearWall = true;
         }
     }
 
     private void OnTriggerExit2D(Collider2D collision)
     {
         if (collision.gameObject.tag == "Wall")
         {
             nearWall = false;
             onWall = false;
         }
     }
 }
  

Below is the parent class for the LizardScript above. This script is where the jumping and standard movement is handled:

 public class PlayerCharacter : MonoBehaviour
 {
     public float speed = 5.0f;
     public Vector2 jumpHeight;
     public bool onGround = true;
 
     // Start is called before the first frame update
     void Start()
     {
         this.tag = "player";
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     public void baseCharacter()
     {
         run();
         jump();
     }
 
     public void run()
     {
         var move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
         transform.position += move * speed * Time.deltaTime;
     }
 
     public void jump()
     {
         if (Input.GetButtonDown("Jump") && onGround == true)
         {
             GetComponent<Rigidbody2D>().AddForce(jumpHeight, ForceMode2D.Impulse);
         }
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             onGround = true;
         }
     }
 
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             onGround = false;
         }
     }
 }

I thought the GetComponent<Rigidbody2D>().gravityScale = 0; in LizardScript would've been enough, but clearly it isn't. If there's anything that I might be missing let me know! Thanks in advance for your time and help!

Comment
Add comment · Show 3
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 Divinitize1 · Jun 24, 2019 at 05:40 AM 1
Share

Have you tried setting the rigidbody2d to Is$$anonymous$$inematic? That should stop so velocity and gravity but still able to be moved via script

avatar image APunkRock · Jun 24, 2019 at 09:12 PM 0
Share

@Divinitize1 Thanks for the suggestion! I had not tried that, but couldn't get it to work. I did however find a way to set the rigidbody2d's velocity to zero and that worked. Which, I swear I had tried before without it working, but I must've put it in the wrong place lol

avatar image Divinitize1 APunkRock · Jun 24, 2019 at 09:13 PM 0
Share

Glad you got it working!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by The_Egyptian · Jun 24, 2019 at 11:18 PM

I'm not 100% sure about this but I think that you problem is that GetComponent<Rigidbody2D>().gravityScale = 0; would only make gravity = 0 but not the falling speed of you object as gravity is an acceleration so it supposed to increase the speed of the falling object every second and when you stop the gravity while the object is still falling it's speed would not be set to 0 only it's acceleration .

so you should add this line too GetComponent<Rigidbody2D> ().velocity = 0; this would set the velocity to 0 just don't used in an update function or it would only freeze your game object

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

231 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

Related Questions

How do I stop my sprite from jumping in the air? 1 Answer

How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer

,Hey! I'm trying to make a Dash move like Celeste but my Dash doesn't seem to work Horizontally(X Axis), My Dash is working perfectly fine Upward and Downwards(Y Axis). 1 Answer

Softbody physics with 2D sprites 1 Answer

Jumping from special jump orb 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