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 SlevinKGames · Apr 22, 2020 at 04:21 PM · scripting problemcollisionphysics2d-platformerjump

2D Platformer with custom physics Object - jumping issue

Hello there :)

I am quite new to unity and coding in general and I am working currently on a 2D-Super-Mario-Like-Platformer Game. I have build an own physics Object with this Unity Tutorial (https://www.youtube.com/playlist?list=PLX2vGYjWbI0SUWwVPCERK88Qw8hpjEGd8) , because it looked promising for exactly the purpose i needed.

Unfortunately I have one issue with my jumping control: When the player jumps and lands on the same level (from Ground to Ground) it behaves as intended. But when i jump on a higher level (e.g. a Plattform) my Player does make another small jump right after landing. As far as I have seen it in a small scene in the youtube Video, the tutor had the very same issue there, but did not notice (or mention) it.

I assume the issue is burried somewhere in the physics Object:

 public float minGroundNormalY = 0.65f;
 public float gravityModifier = 1f;

 public Vector2 targetVelocity;
 public bool grounded;
 protected Vector2 groundNormal;
 public Vector2 velocity;
 protected Rigidbody2D rb2d;
 protected ContactFilter2D contactFilter;
 protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
 protected List<RaycastHit2D> hitbufferList = new List<RaycastHit2D> (16);


 protected const float minMoveDistance = 0.001f;
 protected const float shellRadius = 0.01f;

 private void OnEnable()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }


 // Start is called before the first frame update
 void Start()
 {
     contactFilter.useTriggers = false;
     contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
     contactFilter.useLayerMask = true;
     groundNormal = new Vector2(0f, 1f);
 }

 // Update is called once per frame
 void Update()
 {
     targetVelocity = Vector2.zero;
     ComputeVelocity();
 }

 protected virtual void ComputeVelocity()
 {

 }

 private void FixedUpdate()
 {
     velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
     velocity.x = targetVelocity.x;

     grounded = false;

     Vector2 deltaPosition = velocity * Time.deltaTime;

     Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);

     Vector2 move = moveAlongGround * deltaPosition.x;

     Movement(move, false);

     move = Vector2.up * deltaPosition.y;

     Movement(move, true);
 }

 public void Movement(Vector2 move, bool yMovement)
 {
     float distance = move.magnitude;

     if (distance > minMoveDistance)
     {
         int Count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
         hitbufferList.Clear();
         for (int i = 0; i < Count; i++)
         {
             hitbufferList.Add(hitBuffer[i]);
         }

         for (int i = 0; i < hitbufferList.Count; i++)
         {
             Vector2 currentNormal = hitbufferList[i].normal;
             if (currentNormal.y > minGroundNormalY)
             {
                 grounded = true;
                 if (yMovement)
                 {
                     groundNormal = currentNormal;
                     currentNormal.x = 0;
                 }
             }

             float projection = Vector2.Dot(velocity, currentNormal);
             if (projection < 0)
             {
                 velocity = velocity - projection * currentNormal;
             }

             float modifiedDistance = hitbufferList[i].distance - shellRadius;
             distance = modifiedDistance < distance ? modifiedDistance : distance;
         }
     
     }
     rb2d.position = rb2d.position + move.normalized * distance;
 }


But maybe it is within my player controller:

 public float maxSpeed = 7;
 public float jumpTakeOffSpeed = 7;
 public bool isGrounded = false;

 private SpriteRenderer spriteRenderer;
 private Animator animator;

 public AudioSource jumpSmall;
 public AudioSource jumpBig;


 void Awake()
 {
     spriteRenderer = GetComponent<SpriteRenderer> ();
     animator = GetComponent<Animator> ();
 }

 protected override void ComputeVelocity()
 {
     Vector2 move = Vector2.zero;
     move.x = Input.GetAxis("Horizontal");
     animator.SetFloat("Speed", Mathf.Abs(move.x));

     if (Input.GetButtonDown("Jump") && grounded)
     {
             velocity.y = jumpTakeOffSpeed;
         jumpBig.Play();
     }
     else if (Input.GetButtonUp("Jump"))
     {
         if (velocity.y > 0)
             velocity.y *= 0.5f;
     }

     bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < -0.01f));
     if (flipSprite)
     {
         spriteRenderer.flipX = !spriteRenderer.flipX;
     }

     targetVelocity = move * maxSpeed;

 }



I am completely lost and any help would be very much appreciated :) Thanks Folks!

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 logicandchaos · Apr 22, 2020 at 04:54 PM 0
Share

if you are new to all this you really should just use standard unity stuff, this code looks really over complicated. A rigidbody has its own velocity, gravity everything.. what you did here was reinvent the wheel.. Sometimes people want to do that.. I implemented my own gravity.. but I built it on unity physics..

avatar image SlevinKGames logicandchaos · Apr 22, 2020 at 05:47 PM 0
Share

Hi, thanks for the answer! True, i also thought of leaving all this custom stuff behind and working only with the basic unity stuff. But on the other hand, i really want my game to feel like a 2d Plattformer game and i have read several threads, that it is not the best idea for those games to rely on basic unity 2d physics.

0 Replies

· Add your reply
  • Sort: 

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

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

Sphere get stuck in floor and fails to jump? 1 Answer

Why can't my character jump 2 Answers

Having trouble turning a Transform movement into a Rigidbody force. Code included 1 Answer

Ball jump on collision problem 1 Answer

Ball bounce problem 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