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 engrato · Jul 18, 2013 at 04:23 AM · 2dmovement2d-platformer2d-physics

Yet another 2D pixel perfect movement question

I want to make a precise 2D platormer, something like Thomas Was Alone. The difficulty comes when trying to make a precise pixel perfect collision. I tried several different approaches, but none of them worked out the way I wanted.

These were my attempts:

  1. CharacterController: I just couldn't adapt it to work in this 2D platformer. Its 3D's properties just caused me problems.

  2. Moving the transform: some conflicts with the rest of Unity's physics because I'm moving the transform directly and not the rigidbody.

  3. Changing the rigidbody's velocity: this was what came the closer to what I want, but it seems too unstable. The player keeps shaking and sometimes it bounces when falling.

In all these three options, I tried a lot of combinations with Unity's physics (like gravity, physics materials, rigidbody options).

The following code is my latest attempt (in this case, the game is a runner, so I need to move him to right every frame):

 using UnityEngine;
 using System.Collections;
 
 public class Character : MonoBehaviour
 {
     private float speed = 5.0f;
     private float jumpSpeed = 8.0f;
     private bool isGrounded = false;
     private bool isBlocked = false;
     
     private Transform myTransform;
     private Rigidbody myRigidbody;
     private Vector3 mySize;
     
     protected void Start()
     {
         myTransform = transform;
         myRigidbody = rigidbody;
         mySize = myTransform.localScale;
     }
     
     protected void FixedUpdate()
     {
         CheckCollisions();
         
         if (!isBlocked) {
             if (myRigidbody.velocity.x < speed) {
                 myRigidbody.velocity = new Vector3(speed, myRigidbody.velocity.y, 0);
             }
         }
         else if (myRigidbody.velocity.x != 0) {
             myRigidbody.velocity = new Vector3(0, myRigidbody.velocity.y, 0);
         }
         
         if (isGrounded && Input.GetButtonDown("Jump")) {
             myRigidbody.AddForce(Vector3.up * jumpSpeed, ForceMode.VelocityChange);
             isGrounded = false;
         }
     }
     
     private void CheckCollisions()
     {
         // Correction factor.
         float margin = 0.01f;
         
         Vector3 myPos = myTransform.position;
         
         isGrounded = Physics.Raycast(myPos, -Vector3.up, mySize.y / 2 + margin);
         isGrounded |= Physics.Raycast(myPos - Vector3.right * (mySize.x / 2 - margin), -Vector3.up, mySize.y / 2 + margin);
         isGrounded |= Physics.Raycast(myPos + Vector3.right * (mySize.x / 2 - margin), -Vector3.up, mySize.y / 2 + margin);
         
         isBlocked = Physics.Raycast(myPos, Vector3.right, mySize.x / 2 + margin);
         isBlocked |= Physics.Raycast(myPos - Vector3.up * (mySize.y / 2 - margin), Vector3.right, mySize.x / 2 + margin);
         isBlocked |= Physics.Raycast(myPos + Vector3.up * (mySize.y / 2 - margin), Vector3.right, mySize.x / 2 + margin);
     }
 }

Do you have any suggestions? I'll take anything!

Comment
Add comment · Show 3
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 Benproductions1 · Jul 18, 2013 at 04:25 AM 0
Share

You could try creating your own character controller (I had to do it too). Try looking at Physics.CapsuleCast

avatar image engrato · Jul 20, 2013 at 03:10 AM 0
Share

I guess creating my own controller is what I have left. Using Physics.CapsuleCast seems like a good idea. Thanks!

avatar image Benproductions1 · Jul 20, 2013 at 07:50 AM 0
Share

Just note that when doing this, you must include the "skinWidth" feature from the built-in character controller, or else you will have instances where you will fall through the ground... (I speak from experience ;) )

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

16 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

Related Questions

How do I move a character WITHOUT acceleration/deceleration? 1 Answer

Farseer Physics and Character Control 0 Answers

Weird physics issue after disabling and re-enabling player gameobject. 3 Answers

Jump Code Not Working (2D C#) 2 Answers

2D How to make character slowly stop after moving 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