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 FraiseKiwiBanane · Mar 19, 2017 at 09:17 PM · collisionbug2d-physicsgrid based game

[Unity2D] Collision bug

If I ask today your help is because after hours of$ research on the internet I did not find the answer to my problem:

  • have a grid movement system(pokemon-like) and when I collide with a wall I put my character back on the square where it was before the collision. The problem is that the collision is done before the character is on the square. So I can not move on a square juxtaposed to a wall.

Here is my code (C #):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class Deplacement : MonoBehaviour
 {
     Animator anim;
     Vector3 pos;                                // For movement
     Vector3 lastpos;
     float speed = 2.0f;                         // Speed of movement
     Vector3 localscale;
     GameObject perso;
  
     void Start()
     {
         pos = transform.position;          // Take the initial position
         perso = GameObject.Find("Perso_sprite");
         anim = perso.GetComponent<Animator>();
     }
  
     void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.Q) && transform.position == pos)
         {        // Left
             lastpos = pos;
             pos += Vector3.left;
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", false);
             anim.SetBool ("Right", false);
             anim.SetBool ("Left", true);
             anim.SetBool ("Pause", false);
             anim.SetBool ("Walk", true);
                  
         }
         if (Input.GetKey(KeyCode.D) && transform.position == pos)
         {        // Right
             lastpos = pos;
             pos += Vector3.right;
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", false);
             anim.SetBool ("Right", true);
             anim.SetBool ("Left", false);
             anim.SetBool ("Pause", false);
         }
         if (Input.GetKey(KeyCode.Z) && transform.position == pos)
         {        // Up
             lastpos = pos;
             pos += Vector3.up;
             anim.SetBool ("Up", true);
             anim.SetBool ("Down", false);
             anim.SetBool ("Right", false);
             anim.SetBool ("Left", false);
             anim.SetBool ("Pause", false);
         }
         if (Input.GetKey (KeyCode.S) && transform.position == pos) {        // Down
             lastpos = pos;
             pos += Vector3.down;
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", true);
             anim.SetBool ("Right", false);
             anim.SetBool ("Left", false);
             anim.SetBool ("Pause", false);
         }
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);// Move there
         if(pos == transform.position ){
             anim.SetBool ("Pause", true);
         }
     }
      void OnTriggerEnter2D(Collider2D coll){
         if (coll.gameObject.tag == "Wall")
         {
             transform.position = lastpos;
             pos = lastpos;
             Debug.Log ("Collision");
         }
     }
 }

The box collider off the wall make exactly the size of a square of the grid, and IsTrigger is activated.

The box collider of the wall is exactly the size of a box and it has the function IsTrigger of activated. If I reduce the size of the collider of my character or the wall, my character passes through the wall.

alt text In front of the wall (collider reduced)

alt text When the character go trought the wall

Sorry for the English, I'm french and I'm doing my best.

84560d523a394358985e480c89c41b9a.png (17.6 kB)
95cb80bf029e422881362be7ef14db11.png (17.8 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

130 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

Related Questions

How to generate a raycast on keydown and store what it hits as a variable 0 Answers

3D Jumping Collision Bug 1 Answer

Collider gets sometimes stuck at wall 0 Answers

Why/How 2d tower of blocks collapse? 0 Answers

2D collision stops working if i have the object be a trigger,We're trying to make a 2D platformer, and having a trigger on an object removes it's collision. But, we need it to collide. 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