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 Ed-Den-Gaming · May 29, 2020 at 08:39 AM · unity 5unity 2dtopdownpushpushing

Pushable blocks - dynamic puzzle

Hi. I want to make pushable block puzzles, like you'd see in a Zelda game. My code kind of works but there's always a weird bug/s going on.

The example below is an example of how I'd like it to work. AKA push 1 and 2 up to allow lateral movement with 3

alt text

I'd like it that a player might push block 2 to the right but obviously the block wouldn;t move. Yet that wouldn't then stop them pushing up next. Currently this happens.

I also want to make the block commit to one movement along the relevant axis and not be changed by the player after the push.

I currently get these kinds of issues:

alt text

If a player hits the middle of the block perfectly, it moves as it should but if the corner is hit, bugs happen.

This is my code: public class PushableOneSpace : MonoBehaviour { private Rigidbody2D objectsBody; private float moveSpeed = 2; private Vector3 directionChange; private Vector3 pushFromPosition; private bool playerTouchingBlock; private bool IAmStatic = true; private Vector3 currentPosition; private Vector3 addXPosition; private Vector3 subXPosition; private Vector3 addYPosition; private Vector3 subYPosition; public bool pushable; private bool movingSoNowUntouchable; private float maxPushRange = 1; private void Start() { objectsBody = GetComponent<Rigidbody2D>(); currentPosition = GetComponent<Transform>().position; addXPosition = currentPosition + new Vector3(maxPushRange, 0, 0); addYPosition = currentPosition + new Vector3(0, maxPushRange, 0); subXPosition = currentPosition + new Vector3(-maxPushRange, 0, 0); subYPosition = currentPosition + new Vector3(0, -maxPushRange, 0); } public void MoveBlock() { UpLeftRightDown(transform.position, pushFromPosition); directionChange.Normalize(); objectsBody.MovePosition(transform.position + directionChange * moveSpeed * Time.deltaTime); movingSoNowUntouchable = true; } private void FixedUpdate() { currentPosition = transform.position; if (!IAmStatic) { if (!pushable) { //Works but do you want to keep? objectsBody.bodyType = RigidbodyType2D.Static; return; } if (playerTouchingBlock && pushable) { objectsBody.bodyType = RigidbodyType2D.Dynamic; MoveBlock(); //BELOW here is only applicable when the player has finished moving, not during! if (currentPosition.x >= addXPosition.x) { Debug.Log(">= add X"); pushable = false; } else if (currentPosition.x <= subXPosition.x) { Debug.Log("<= sub X"); pushable = false; } else if (currentPosition.y >= addYPosition.y) { Debug.Log(">= add Y"); pushable = false; } else if (currentPosition.y <= subYPosition.y) { Debug.Log("<= sub Y"); pushable = false; } } else { Debug.Log("Want To Return RETURN"); return; } } else { objectsBody.bodyType = RigidbodyType2D.Static; } } private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("PlayerPush")) { if (!movingSoNowUntouchable) { IAmStatic = false; pushFromPosition = other.transform.position; playerTouchingBlock = true; } } } private void OnTriggerStay2D(Collider2D other) { if (other.CompareTag("PlayerPush")) { if (!movingSoNowUntouchable) { IAmStatic = false; pushFromPosition = other.transform.position; playerTouchingBlock = true; } } } private Vector3 UpLeftRightDown(Vector3 positionOne, Vector3 postionTwo) { float diffferenceY = positionOne.y - postionTwo.y; float diffferenceX = positionOne.x - postionTwo.x; if (Mathf.Abs(diffferenceY) > Mathf.Abs(diffferenceX)) { if (diffferenceY > 0) { objectsBody.constraints = RigidbodyConstraints2D.FreezePositionX; objectsBody.freezeRotation = true; Debug.Log("Touching Bottom"); return directionChange = transform.position + new Vector3(0, 1, 0); } else if (diffferenceY < 0) { objectsBody.constraints = RigidbodyConstraints2D.FreezePositionX; objectsBody.freezeRotation = true; Debug.Log("Touching Up"); return directionChange = transform.position + new Vector3(0, -1, 0); } else { Debug.Log("Touching Nothing UPDown"); objectsBody.constraints = RigidbodyConstraints2D.None; objectsBody.freezeRotation = true; return directionChange = transform.position + new Vector3(0, 0, 0); } } else { if (diffferenceX > 0) { Debug.Log("Touching Left"); objectsBody.constraints = RigidbodyConstraints2D.FreezePositionY; objectsBody.freezeRotation = true; return directionChange = transform.position + new Vector3(1, 0, 0); } else if (diffferenceX < 0) { Debug.Log("Touching Right"); objectsBody.constraints = RigidbodyConstraints2D.FreezePositionY; objectsBody.freezeRotation = true; return directionChange = transform.position + new Vector3(-1, 0, 0); } else { Debug.Log("Touching Nothing Sides"); objectsBody.constraints = RigidbodyConstraints2D.None; objectsBody.freezeRotation = true; return directionChange = transform.position + new Vector3(0, 0, 0); } } } } Thanks

untitled.png (8.2 kB)
block-movement-2.gif (363.5 kB)
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

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

228 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

Related Questions

I can't give damage with these scripts 2 Answers

Platform effector bounciness 0 Answers

How can i change the sprite image of button in run time of a 2d game 1 Answer

Virtual Joystick jump in unity2d 0 Answers

triplex 2d unity game 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