• 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 /
This question was closed Jan 30, 2016 at 07:40 PM by GeoDragn for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GeoDragn · Jan 27, 2016 at 05:59 PM · 2d-platformer

Platformer Character

So I've been working on a little platformer, and I've been trying to set up custom physics for the player. It works fine, until I add the ability to run. He seems to be going faster than my physics can keep up with. He keeps going into walls and slopes. Any idea how I should fix it? Thanks!

 public float Y_Velocity;
 public float X_Velocity;
 private string dir;
 private bool jumpKey;
 public bool onGround;
 public LayerMask mask;

 void Start ()
 {
     
 }
 
 void FixedUpdate ()
 {
     transform.rotation = Quaternion.Euler(0, 0, 0);
     if (Input.GetKey("z") && Y_Velocity > 5.5f)
     {
         Y_Velocity = Y_Velocity - 0.5f;
     }
     else
     {
         Y_Velocity = Y_Velocity - 1;
     }        
     transform.Translate(Vector3.up * Time.deltaTime * Y_Velocity);
     if (!Physics2D.Raycast(transform.position + new Vector3(-0.2f, -1f, 0), Vector3.right, 0.4f, mask))
     {
         onGround = false;
     }
     if (Y_Velocity > 0)
     {
         while (Physics2D.Raycast(transform.position + new Vector3(-0.2f, 0.55f, 0), Vector3.right, 0.4f, mask))
         {
             Y_Velocity = 0;
             transform.Translate(Vector3.down * Time.deltaTime);
         }
     }
     if (Y_Velocity < 0)
     {
         while (Physics2D.Raycast(transform.position + new Vector3(-0.2f, -0.55f, 0), Vector3.right, 0.4f, mask))
         {
             Y_Velocity = 0;
             transform.Translate(Vector3.up * Time.deltaTime);
             onGround = true;
         }
     }
     if (Input.GetKey("z") && jumpKey == false && Physics2D.Raycast(transform.position + new Vector3(-0.2f, -0.65f, 0), Vector3.right, 0.4f, mask))
     {
         Y_Velocity = 15;
         jumpKey = true;
     }
     X_Velocity = X_Velocity * 0.7f;
     transform.Translate(Vector3.right * Time.deltaTime * X_Velocity * 3);
     if (Input.GetKey("right"))
     {
         if (Input.GetKey("a"))
         {
             X_Velocity = X_Velocity + 1;
         }
         X_Velocity = X_Velocity + 1;
         dir = "right";
     }
     else if (Input.GetKey("left"))
     {
         if (Input.GetKey("a"))
         {
             X_Velocity = X_Velocity - 1;
         }
         X_Velocity = X_Velocity - 1;
         dir = "left";
     }
     if (X_Velocity > 0)
     {
         while (Physics2D.Raycast(transform.position + new Vector3(0.25f, 0.55f, 0), Vector3.down, 0.9f, mask))
         {
             X_Velocity = 0;
             transform.Translate(new Vector3(-0.1f, 0, 0) * Time.deltaTime);
         }
     }
     if (X_Velocity < 0)
     {
         while (Physics2D.Raycast(transform.position + new Vector3(-0.25f, 0.55f, 0), Vector3.down, 0.9f, mask))
         {
             X_Velocity = 0;
             transform.Translate(new Vector3(0.1f, 0, 0) * Time.deltaTime);
         }
     }
     if (dir == "left")
     {
         transform.rotation = Quaternion.Euler(0, 180, 0);
     }
     if(!Input.GetKey("z"))
     {
         jumpKey = false;
     }
 }
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

  • Sort: 
avatar image
0
Best Answer

Answer by FortisVenaliter · Jan 27, 2016 at 06:28 PM

For those collision checks, you need to factor in the speed instead of using hard-coded values. When the character is moving faster, your collision checks need to go further. Also, instead of stopping dead when you detect a collision, maybe move the character to the point of collision before stopping them.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C#: Trying to debug.log message when player collides with object 0 Answers

How to do 2D collsision detection most efficiently? 1 Answer

OnTriggerEnter Getting called more than once. 0 Answers

How do i make my player sprite change, when entering a powerup? 1 Answer

Poor quality sprite 2D game 3 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