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 /
  • Help Room /
avatar image
0
Question by williamlitwa · May 21, 2016 at 04:13 PM · rigidbody2dpixeljittertop down2d physics

2D Physics and Pixel Perfect Movement?

Hi,

New to Unity. So far it seems absurdly easy to develop in, but I cannot for the life of me get pixel perfect movement with physics down.

Game is a top down adventure RPG (think LTTP crossed with FFVI). 8 Direction free character movement. Importantly, the game features 2x or 4x pixel graphics. If it wasn't for that, this would all be much easier.

So far I have this (simplified):

 public float speed = 1f;
 private BoxCollider2D boxCollider;
 private RaycastHit2D hit;

 void Awake()
 {
     boxCollider = GetComponent<BoxCollider2D>();
 }

 public void Move(float move_x,float move_y)
 {
     var direction = new Vector2(move_x, move_y) * speed;
     hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, direction.y), Mathf.Abs(direction.y));
     if (hit.collider == null)
     {
         transform.Translate(0, direction.y, 0);
     }
     hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(direction.x, 0), Mathf.Abs(direction.x));
     if (hit.collider == null)
     {
         transform.Translate(direction.x, 0, 0);
     }
 }

Player has a box collider, objects have a 2d box or polygon collider.

This elegantly solves the problem of having pixel perfect movement as each pixel is a unit. The camera translates directly. No jitter, no issues. If anyone is reading this and needs pixel perfect movement and isn't worried about physics, this works great.

But because of the way I'm raycasting a box, if the player hits an angle, they stop and don't bounce off of it or slide up it. It stops the player in their tracks the same as a flat wall. There aren't any physics at play here. Pardon the pun.

e.g. I'm moving to the right and hit a slope /. Rather than the player travelling along it, northwards, the player stops in their tracks as if hitting a flat wall |. To move further, I have to move the player UP and then to the RIGHT.

If I try to use any physics (such as Rigidbody2D.velocity), without using Translate, the player doesn't move in a pixel perfect fashion anymore. The player bounces off walls like they need to, but the whole image stutters across the screen and the player doesn't stick to the pixel grid.

So my question is twofold:

One, is there any way to use 2D physics (a la Rigidbody2D, etc) and get pixel perfect movement?

Or, two, is there any way to modify the above to build in my own physics for bouncing off / sliding across angled walls?

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 LLIV · May 21, 2016 at 11:49 PM

Your script won't bounce off of objects because there is no physics. As for getting this movement your options aren't great. One thing that may work great for you is PixelBoy.cs

 using UnityEngine;
 using System.Collections;
 
 namespace Nixon.Cameras.Effects
 {
     //PIXELBOY BY @WTFMIG EAT A BUTT WORLD BAHAHAHAHA POOP MY PANTS
     [ExecuteInEditMode]
     [AddComponentMenu ("Image Effects/PixelBoy")]
     /*PIXELBOY BY @WTFMIG EAT A BUTT WORLD BAHAHAHAHA POOP MY PANTS
      * "Yes that is his actual copyright"
      * Used to pixelate the screen in a fairly cheap way */
     public class PixelBoy : MonoBehaviour
     {
         public int w = 720;
         int h;
         public Camera Camera;
 
         protected void Start ()
         {
             if (!SystemInfo.supportsImageEffects) {
                 enabled = false;
                 return;
             }
             // + + + +  Added by Louis Lombardo IV + + + +
             if (Camera == null) {
                 try { 
                     Camera = GetComponent<Camera> ();
                 } catch (UnassignedReferenceException e) {
                 }
             }
             if (Camera == null) {
                 this.enabled = false;
             }
             // = = = = = = = = = = = = = = = = = = = = = =
         }
 
         void Update ()
         {
 
             float ratio = ((float)Camera.pixelHeight / (float)Camera.pixelWidth);
             h = Mathf.RoundToInt (w * ratio);
 
         }
 
         void OnRenderImage (RenderTexture source, RenderTexture destination)
         {
             source.filterMode = FilterMode.Point;
             RenderTexture buffer = RenderTexture.GetTemporary (w, h, -1);
             buffer.filterMode = FilterMode.Point;
             Graphics.Blit (source, buffer);
             Graphics.Blit (buffer, destination);
             RenderTexture.ReleaseTemporary (buffer);
         }
     }
 }

 

This way no matter what your entire screen would be pixelated. You wouldn't have to worry about pixel perfect movement or pixel perfect anything for that matter because it would all just be pixelated anyway.

Your other option would have to be something really, really, sketchy... something like having a separate object that you calculate physics with but only apply changes in position if it lines up with your pixel grid.

Use pixelboy.

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 zorksox · Jun 05, 2017 at 09:51 PM

I know this is an old tread, but I think you could achieve this by having the visual representation of the object aligned to the grid, while the actual object has a floating point transition. In other words, make the player a 2d physics object, free to move and bounce anywhere, but the sprite that the player sees is shifted to fit on the grid by rounding the position components to your unit width. This could cause pixel-floating when the character stands on the edge of a cliff, but making the collider 1 unit smaller in both directions could fix this.

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

55 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

Related Questions

Pixel jitter/flicker while camera movement (2D) 2 Answers

Mirror Networking Moving platform jittering 0 Answers

Rigidbody2D moving WAY to fast 1 Answer

Pixel Perfect Camera makes my player stutter 0 Answers

Unity Perfect Bounce 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