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 BigAlecz · Aug 09, 2019 at 02:49 PM · unity 2dprogrammingplatformerplayer movement

Platformer problems

I am currently in the process of developing my first platformer, and I've decided that I want to bake my own acceleration and deceleration for movement instead of using unity's wonky physics. ........................................................................................................................................................... This has brought up two roadblocks, one being that my character will just clip through objects like walls to jump over or off of, and the other is that if I try to switch directions while decelerating, my character will turn around, but it won't change directions and my acceleration will bypass the limit I set for it (in layman's, it zooms backwards). ................................................................................................................................................................. I need help fixing this and can describe any of the functions of the variables if asked. There is no physics material on my character, and I am using physics for the jump. ............................................................................................................................................................. All of the variables are defined in the unity editor for easier tweaking except for currentSpeed, jumpCount, FallTime and canJump, which I have serialized so I can observe them during testing.

 using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class PlayerControls : MonoBehaviour { //walk Vars [SerializeField] float currentSpeed; [SerializeField] float maxSpeed; [SerializeField] float jumpstart; [SerializeField] float acceleration; [SerializeField] float deceleraton; int isFlip = 1; Vector2 newPos; Quaternion flip;

 //jump Vars 
 [SerializeField] public int jumpCount;
 [SerializeField] public int jumpCountMax;
 [SerializeField] public float jumpVel;
 [SerializeField] public float fallMulti;
 [SerializeField] public float shortMulti;
 [SerializeField] public float fallTime;
 [SerializeField] public float coyoteTime;
 [SerializeField] bool canJump = true;
 [SerializeField] bool didJump;

 //references
 Rigidbody2D rb;

 private void Awake() {
     rb = GetComponent<Rigidbody2D>();
 }

 //walk Functions
 private void speedCalc() {
     currentSpeed += jumpstart * isFlip;
     if (currentSpeed * isFlip >= maxSpeed) {
         currentSpeed = maxSpeed * isFlip;
     }
     else {
         currentSpeed = (currentSpeed * acceleration);
     }
 }

 private void Start() {
     currentSpeed = 0;
 }

 private void Update() {
     //calculate coyote time
     if (fallTime == coyoteTime) {
         if (didJump == false) {
             jumpCount += 1;
         }
     }
     if (jumpCount >= jumpCountMax) {
         canJump = false;
     } else {
         canJump = true;
     }
     if (rb.velocity.y == 0) {
         jumpCount = 0;
         didJump = false;
     }

     //calculate isFlip
     if (Input.GetButton("Right")) {
         flip = Quaternion.Euler(0, 0, 0);
         transform.rotation = flip;
         isFlip = 1;
     } else if (Input.GetButton("Left")) {
         flip = Quaternion.Euler(0, 180, 0);
         transform.rotation = flip;
         isFlip = -1;
     }

     //calculate acceleration & deceleration
     if (!Input.GetButton("Right") && !Input.GetButton("Left")) {
         if (currentSpeed * isFlip <= jumpstart) {
             currentSpeed = 0;
         } else {
             currentSpeed = (currentSpeed / deceleraton);
         }
     } else {
         speedCalc();
     }

     //implement falling and dyanamic jumping
     if (rb.velocity.y < 0) {
         fallTime += 1;
         rb.velocity += Vector2.up * Physics2D.gravity * (fallMulti - 1) * Time.deltaTime;
     } else if (rb.velocity.y == 0) {
         fallTime = 0;
     }  else if (rb.velocity.y > 0 && !Input.GetButton("Jump")) {
         rb.velocity += Vector2.up * Physics2D.gravity * (shortMulti - 1) * Time.deltaTime;
     }

     //execute jump
     if (Input.GetButtonDown("Jump") && canJump == true) {
         didJump = true;
         rb.velocity = Vector2.up * jumpVel;
         jumpCount += 1;
     }

     //move character position
     newPos = new Vector2(transform.position.x + currentSpeed, transform.position.y);
     transform.position = newPos;
 }

}

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

Answer by ludriloh · Aug 09, 2019 at 05:19 PM

Unity made a really good tutorial about this exact thing, they made custom physics for a 2d game to make it feel a lot better. They also went over all the collision problems in it. Here's the link, it also has all the code in the bottom, I think it'll help. https://learn.unity.com/tutorial/live-session-2d-platformer-character-controller#

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 BigAlecz · Aug 09, 2019 at 06:13 PM 0
Share

Thank you my guy. Ill take a look.

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

127 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

Related Questions

Multiple Cars not working 1 Answer

Pause System not working :/ 2 Answers

2D Platformer Bug 0 Answers

how to move a gameobject only on the x axis? 1 Answer

"the object of type transform has been destroyed but you are still trying to access it" 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