Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rain478293 · Feb 02, 2021 at 10:24 PM · unity 2drigidbody2dvelocitycharacter movementfalling through terrain

Weird velocities (bug?) when moving character in unity 2D,

So i'm trying to make simple platformer in unity 2020.2.2f1. I made a character controller but it doesn't work properly. Even though player jumps and moves in every direction there is something weird going on. It all started when i tried implementing jumping. I use addforce for jumping and when player is falling he sometimes falls through ground for a second, creating bouncy effect. it's really weird. I tried changing collsion mode to continous and basically everything i could find but nothing works. What i found out is velocities of player are acting really weird. Their value sometimes is equal to billions below zero. I forgot to mention that i'm using simplified version of Brackeys movement script ( without crouching etc).For all this weird behaviors i blame Vector3.SmoothDamp but i don't have much experience. Here is the code :

.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Movement : MonoBehaviour {

   public float speed = 0.01f;
 public float JumpForce = 2f;
 private Vector3 velocity = Vector3.zero;
  
 void Start()
 {
     
 }


 void FixedUpdate()
 {
     Rigidbody2D rb = GetComponent<Rigidbody2D>();
     if (Input.GetKeyDown(KeyCode.Space))
     {
         rb.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
     }
     float horizontal = Input.GetAxisRaw("Horizontal");
     
     Vector3 targetVelocity = new Vector2(speed * horizontal, rb.velocity.y);
     rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, 0.3f);
    




 }

 private void Update()
 {

     Rigidbody2D rb = GetComponent<Rigidbody2D>();
     if (Input.GetKeyDown(KeyCode.Space))
     {
         rb.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
     }
 }

} ,

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GDGames0302 · Feb 03, 2021 at 01:04 PM

Hi. I'm not sure if this helps you, but I have a suggestion: create a physics material 2d and change the bounciness to 0. Then assign that physics material 2d to your rigidbody2d. Also, you should use FixedUpdate only for physics(raycasts...), not for movement. You should implement the movement in Update. A tip for optimization: use Rigidbody2D rb = GetComponent(); in Start because you're assigning that rigidbody2d every frame. Also, you are jumping in both FixedUpdate and Update and that may cause the issue too. Jump only in Update.

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
0

Answer by Rain478293 · Feb 03, 2021 at 11:00 PM

@GDGames0302 I did what you said and nothing changed, i even tried removing smoothdamp part but it's still same. Everything is acting really weird, not really sure why. Just found out that angular velocity is broken too. it's changing to few billions below zero just like other velocities. When player is standing, there is this weird wave effect of pixels moving upwards and everything else is as werid as it was.

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

120 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

Related Questions

Player is not moving 0 Answers

Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer

,Rotate a gameobject around another while being attracted by its gravity 1 Answer

Rigidbody2D giving incorrect value 1 Answer

Velocity Randomly resets to (0, 0) 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