Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by bracciata · Jul 29, 2015 at 07:01 PM · c#2d2d-platformerphysics2d

While Moving Left or Right my character falls more slowly.

Here's my script and also I should mention upon letting go of left or right buttons fall speed greatly increases. Also while your here I did use the unity 2d platformer tutorial to check if grounded and if grounded is not working.

     public GameObject MarioMobster;
     public Vector3 RightForce = new Vector3(404, 0);
     public Vector3 leftforce = new Vector3(-404, 0);
     public Vector3 Jumpforce = new Vector2 (0, 404);
     public static int health = 3;
     public Sprite sprite1;
     public Sprite sprite2;
     private SpriteRenderer spriterenderer;
     //public GameObject bullets;
     private Transform groundcheck;
         private bool grounded = false;
     private bool LookingRighty = true;
     private bool LookingLefty = false;
     public float bulletspeed = 20f;
     public Rigidbody2D bullets;
     // Use this for initialization
     void Start () {
         spriterenderer = GetComponent<SpriteRenderer> ();
         if (spriterenderer.sprite == null) {
             spriterenderer.sprite = sprite1;
         }
     }
     
     // Update is called once per frame
     void Update () {
         grounded = Physics2D.Linecast(transform.position, groundcheck.position, 1 << LayerMask.NameToLayer("Ground"));
     }
     void Awake(){
         groundcheck = transform.Find ("groundcheck");
     }
     public void Right (){
         GetComponent<Rigidbody2D>().velocity = RightForce;
         //GetComponent<Rigidbody2D>().AddForce(RightForce);
         if (LookingLefty = true) {
             LookingLefty = false;
             LookingRighty = true;
         }
         Debug.Log ("moveScript.Right was called succesfully");
         if(spriterenderer.sprite == sprite2){
             spriterenderer.sprite = sprite1;
 
         }
 
     }
     public void Left(){
         GetComponent<Rigidbody2D> ().velocity = leftforce ;
         Debug.Log ("left has also been called");
         if (LookingRighty = true) {
             LookingRighty = false;
             LookingLefty = true;
         }
                 if(spriterenderer.sprite == sprite1){
                     spriterenderer.sprite = sprite2;
                 }
     }
     public void Jump(){
         Debug.Log ("jump has also been called");
         if (grounded) {
             GetComponent<Rigidbody2D> ().velocity = Jumpforce;
             Debug.Log("grounded is true");
         }
     }
 
 }

Comment
Add comment · Show 2
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 DerpMerf · Jul 29, 2015 at 07:03 PM 1
Share

Why are rightForce and leftForce Vector3s? $$anonymous$$ake them Vector2s and try it again

avatar image Glurth · Jul 30, 2015 at 12:23 AM 0
Share

Agree with Derp$$anonymous$$erf. JumpForce too.

2 Replies

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

Answer by 3DDean · Jul 30, 2015 at 03:31 AM

The reason the character is falling more slowly is because you are setting the y velocity to zero. So the physics update pulls it down. But every frame you set the velocity to zero. So to fix it maybe try using add force instead of setting the velocity directly. That should fix it. Then you said that checking if you are grounded is not working? Is unity giving any errors?

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

Answer by TheHeron · Jul 30, 2015 at 12:31 PM

Looking at your code, I assume you removed AddForce because it was repeatedly adding force as long as you were holding right or left.

What you most likely want to do is instead use something like this:

 [...] .velocity = new Vector3(RightForce.x,OurRigidBody2D.velocity.Y);

This way you maintain your user's current vertical velocity as per the physics system while keeping horizontal movement.

Also, as a quick aside that might help you, you don't want to be calling GetComponent every time your user moves right or left and such - it's a rather slow operation. Consider instead using the Awake() or Start() methods built into MonoBehaviour to store a permanent reference to your GameObject's RigidBody2D. You've already done this with your SpriteRenderer, the process is essentially identical. Then when you're using it in the Right/Left/Jump parts of your code, you'd instead use OurRigidBody2D.velocity or whatever you called your reference to your RigidBody2D.

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

24 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

Related Questions

Unity 2D - Rigidbody2D Velocity sudden freeze 0 Answers

Multiple Cars not working 1 Answer

Boss Health Bar 1 Answer

Photon position syncing 0 Answers

Physics2D checking if object goes out of OverlapArea 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